Zet
April 7, 2022, 11:45am
1
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):
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?
cualr
April 7, 2022, 1:13pm
2
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.
Zet
April 7, 2022, 1:50pm
3
Are u able to show me how its done? I can’t seem to get it to work.
cualr
April 7, 2022, 9:00pm
4
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()
Zet
April 8, 2022, 4:03am
5
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:
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
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,
Snehan
Zet
April 8, 2022, 8:39am
7
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 .
cualr
April 8, 2022, 1:03pm
9
Have you tried the “text_auto=True”?
fig = px.bar(itemcrosstab,barmode=‘group’, title=“Long-Form Input”,text_auto=True)
My output.
1 Like
Zet
April 8, 2022, 1:09pm
10
@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
system
Closed
April 8, 2023, 1:10pm
11
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.