How to compare data/scores of multiple products

I have scraped all the scores of products in different areas. Now I would like to make comparison of these scores in different areas with selected products. I was thinking of using bar charts (due to lack of imagination and try to do it as simple as possible first).

However, I can’t understand how I select different rows (where the scores are) from the excel file and display this in a bar chart. Would like to know if someone give me some advice

Hey @gplm_beginner !

In Python, the preferred way to manipulate data is using the Pandas library.

  • there is a function for reading an Excel file into a Pandas dataframe. This article may also help you load the data and install some optional dependencies. So for example df = pd.read_excel("path/to/file.xlsx").
  • When your Excel data is in a DataFrame, you will look on how to filter rows depending on a product. There are guides for this in the documentation, here’s one from the 10 minute quickstart. The query would look like df_selected_by_column = df[df['product'] =="your_product"]
  • Finally if you are looking for the simple bar chart you can try Streamlit’s builtin bar_chart, you may need to filter your columns a bit beforehand, like st.bar_chart(df_selected_by_column[["product", "value"]).

If you want users to give you a bit more specific advice, you could provide us with part of the Excel file and a chart example you would want to display :wink:.

Cheers,
Fanilo

1 Like

@andfanilo thanks for your reply.

My excel table looks like attached screenshot

and i read the excel file:

@st.cache
def load_data():
    df=pd.read_excel("Location")
    #df=df[df['Price']>0]
    data = df
    return data
data = load_data()

I just don’t know how I can display the scores of Product A and Product B in a bar chart. So I can compare who is better.

Hey @gplm_beginner, you’re free to use one of the many graph libraries Streamlit supports (Matplotlib/Seaborn, Altair, Plotly, Bokeh to name a few).

For example here is an interactive one using Plotly Express Grouped bar chart with st.plotly_chart:

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

# continue loading the data with your excel file, I was a bit too lazy to build an Excel file :)
df = pd.DataFrame(
    [["Product A", 5.6, 7.8, 5], ["Product B", 5.8, 7.2, 4.9]],
    columns=["Product", "Comfort", "Sound", "Calls"]
)

fig = px.bar(df, x="Product", y=["Comfort", "Sound", "Calls"], barmode='group', height=400)
# st.dataframe(df) # if need to display dataframe
st.plotly_chart(fig)

Afterwards it becomes more of a reading the graph library documentation (notably because I guess you’d prefer to group by score rather than product?) though rather than a Streamlit question…but feel free to edit the code and see the graph result in Streamlit in realtime to better understand what happens when you edit things!

Good luck !
Fanilo

4 Likes

So I did this months ago but I couldn’t find it x.x but the info is somewhere in Styling plotly express figures in Python and Setting the font, title, legend entries, and axis titles in Python :

  • There’s a labels argument in the plotly_express call you can use to edit x/y axis labels, something like px.bar(df=df, x=x, y=y, labels={"variable": "Product count"}) for example.

  • You also have fig.update_yaxes which I think let’s you edit the text of the title if you find the correct argument:

fig.update_yaxes( 
    title_text="Product Count"
)

Hope you can find it out, let us know if you need a bit more detail

Fanilo :balloon:

Hi , would it be possible if you can create the excel file and build the code again sourcing the data through excel.

Would be really appreciated