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