Help Pie chart Plotly

Hey! I started using Plotly just recently and Iā€™m trying to plot data of a supply chain most sale categories with the next code:

cat = data.groupby([ā€˜Category Nameā€™])[ā€˜Sales per customerā€™].sum().reset_index().sort_values(ā€˜Sales per customerā€™)
fig = px.pie(cat, values=ā€˜Sales per customerā€™, names=ā€˜Category Nameā€™, title=ā€˜Categorias con mas ventasā€™)
fig.show()

And I get all the categories but Ijust want the top 10

Welcome to Dash @eric7
Maybe you can filter your dataframe before creating the figure, so it has the top 10 of the category you want. For example, for top 10 ā€˜Sales per customerā€™:

cat = data.groupby([ā€˜Category Nameā€™])[ā€˜Sales per customerā€™].sum().reset_index().sort_values(ā€˜Sales per customerā€™)
cat_top_10 = cat.nlargest(10, "Sales per customer")
fig = px.pie(...
1 Like

Hey Adam thank you it worked just fine!! this is how I did:

cat = data.groupby(['Category Name'])['Sales per customer'].sum().reset_index().sort_values('Sales per customer')
cat_top_10 = cat.nlargest(10, "Sales per customer")
fig = px.pie(cat_top_10, values='Sales per customer', names='Category Name', title='Categorias con mas ventas')
fig.show()

1 Like