Plot count of one column versus time grouped by two columns

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

Upvotes: 1

Views: 1075

Answers (3)

This could also be one way to do this. It is useful to plot aggregated data (count, sum,mean etc.) when a dataframe is grouped by two columns and we want to represent both columns in the plot. Also, 'facet_col' and 'color' could be interchanged depending on desired output.

The plot doesn't look correct with dummy data but it worked with my actual data.

import plotly.express as px

px.bar(df_grouped, x='month', y='count', color='Company',facet_col='Product')

enter image description here

Upvotes: 0

Lucas M. Uriarte
Lucas M. Uriarte

Reputation: 3101

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")

enter image description here

Upvotes: 2

kjanker
kjanker

Reputation: 61

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)

enter image description here Note that you have to reset the index to remove the multi index (currently not supported by plotly express).

Upvotes: 2

Related Questions