I want to create a bar graph showing count of top three Products per month grouped by Company. Please suggest a way to achieve this, preferably using plotly-express library.
data = [['2017-03', 'Car','Mercerdes'], ['2017-03', 'Car','Mercerdes'], ['2017-03', 'Car','BMW'],['2017-03', 'Car','Audi'],
['2017-03', 'Burger','KFC'], ['2022-09', 'Burger','Subway'], ['2022-09', 'Coffee','Subway'],
['2017-03', 'Clothes','Only'], ['2022-09', 'Coffee','KFC'], ['2022-09', 'Coffee','McD']
]
df = pd.DataFrame(data, columns=['Month', 'Product','Company'])
df
Month Product Company
0 2017-03 Car Mercerdes
1 2017-03 Car Mercerdes
2 2017-03 Car BMW
3 2017-03 Car Audi
4 2017-03 Burger KFC
5 2022-09 Burger Subway
6 2022-09 Coffee Subway
7 2017-03 Clothes Only
8 2022-09 Coffee KFC
9 2022-09 Coffee McD
CodePudding user response:
You can do this with pandas groupby
and size
.
import plotly.express as px
data = df.groupby(["Month", "Company", "Product"]).size().rename("Count").reset_index()
month = "2017-03"
px.bar(data[data["Month"]==month], x="Product", y="Count", color="Company", title=month)
Note that you have to reset the index to remove the multi index (currently not supported by plotly express).
CodePudding user response:
The approach of groupby done by @kjanker is perfect have the number of counts. I think later on in plotly you can use barmode=group and add the text to the bars as product yields something similar to what you look for.
Otherwise I don't think with plotly express you can achieve to split the columns by Company and Product
import plotly.express as px
data = data = df.groupby(["Month", "Company", "Product"]).size().rename("Count").reset_index()
px.bar(data, x="Month", y="Count", color="Company",
barmode='group', text="Product")