How to display the count on the Bar Charts?

Hi, I want to display the the number of counts for each of the bar charts. Currently Iā€™m able to plot the bar chart for each of the ratings(1 to 5) for an item, but i couldnā€™t work out how to display the counts.

Similar to the output below(60,129,142,106,42):
image

My code as below:

import streamlit as st
import pandas as pd
import plotly.express as px

@st.cache
def get_data(filename):
ratings = pd.read_csv(filename)
return ratings
ratings = get_data(ā€˜item.csvā€™)

itemcol = ratings[ā€˜Item_Nameā€™]
ratingcol = ratings[ā€˜Rating_Scoreā€™]
itemcrosstab = pd.crosstab(itemcol, ratingcol).value_counts()
fig = px.bar(itemcrosstab,text=itemcrosstab, barmode=ā€˜groupā€™)
fig.show()

However I got this error:

The error is at this line of code:
itemcrosstab = pd.crosstab(itemcol, ratingcol).value_counts()

How do i resolve this?

The issue is MultiIndex, you can solve this problem by extracting the values you want to display and save them as a list. Then substitute the list for the itemcrosstab section.

Are u able to show me how its done? I canā€™t seem to get it to work.

I didnā€™t see your dataset so I created this basic concept, you will need to do some cleaning changes for your dataset but it should work.

import streamlit as st
import pandas as pd
import plotly.express as px

st.write(ā€˜testā€™)

data = [[ā€˜Star 1ā€™, 1], [ā€˜Star 2ā€™, 1], [ā€˜Star 3ā€™, 1],
[ā€˜Star 1ā€™, 1], [ā€˜Star 2ā€™, 1], [ā€˜Star 3ā€™, 1]]

df2 = pd.DataFrame(data, columns = [ā€˜Starā€™, ā€˜Voteā€™])
itemcrosstab=pd.crosstab(df2.Star,df2.Vote)
itemcrosstab_text=itemcrosstab[1].to_list()
fig = px.bar(itemcrosstab.index, title=ā€œLong-Form Inputā€,text=itemcrosstab_text)
fig.show()

my dataset(30):

Item_Name Rating_Score
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 3
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 3
Samsung Galaxy S20 FE 5G 4
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 4
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 1
Samsung Galaxy S20 FE 5G 1
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 1
Samsung Galaxy S20 FE 5G 4
Samsung Galaxy S20 FE 5G 3
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 1
Samsung Galaxy S20 FE 5G 4
Samsung Galaxy S20 FE 5G 2
Samsung Galaxy S20 FE 5G 4
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 4
Samsung Galaxy S20 FE 5G 1
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 1
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 4
Samsung Galaxy S20 FE 5G 5
Samsung Galaxy S20 FE 5G 5

It only manage to display the first bar count correctly as follows:
image

I tried to append the remaining the index[2],[3],[4],[5] into a list but it didnā€™t work.

Any idea how to solve this?

Hi @Zet :wave:

Hereā€™s my solution that doesnā€™t use crosstab:

import streamlit as st
import pandas as pd
import plotly.express as px
import numpy as np

df = pd.DataFrame(
    np.random.randint(1, 6, size=(100, 2)), columns=["Item_Name", "Rating_Score"]
)

df = (
    df.groupby("Rating_Score")
    .count()
    .reset_index()
    .rename(columns={"Item_Name": "Count"})
)
df["Item_Name"] = "Samsung Galaxy S20 FE 5G"
st.dataframe(df)

fig = px.bar(
    df,
    x="Rating_Score",
    y="Count",
    color="Rating_Score",
    text="Count",
)

st.plotly_chart(fig)

Best, :balloon:
Snehan

Hi @snehankekre,
Is it not possible to solve it using crosstab? Because my code also consists of asking the user input as there will be more than 1 item.

import streamlit as st
import pandas as pd
import plotly.express as px

@st.cache
def get_data(filename):
    ratings = pd.read_csv(filename)
    return ratings

ratings = get_data('item.csv')

item_list = ratings.iloc[:, 1].unique().tolist()
user_option = st.multiselect(ā€œSelect Itemsā€,item_list,default=item_list[0])
filteritem = ratings[ratings[ā€˜Item_Nameā€™].isin(user_option)]
itemcrosstab = pd.crosstab(filteritem , ratings[ā€˜Rating_Scoreā€™])
itemcrosstab_text = itemcrosstab.iloc[:,0].tolist()
fig = px.bar(itemcrosstab, text=itemcrosstab_text,barmode=ā€˜groupā€™)
fig.show()

Iā€™m stuck at this line of code where it only displays the first bar chart count correctly:
itemcrosstab_text = itemcrosstab.iloc[:,0].tolist()

Please advise, thank you.

Iā€™m sure it is possible, but I currently donā€™t know how. You could inquire in the Plotly Python forum.

Have you tried the ā€œtext_auto=Trueā€?

fig = px.bar(itemcrosstab,barmode=ā€˜groupā€™, title=ā€œLong-Form Inputā€,text_auto=True)

My output.

1 Like

@cualr Oh wow, it works. Canā€™t believe its juz adding this text_auto in the parameter. Thought would be more complicated. Thank you!

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.