Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group small values in a pie chart

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df= pd.DataFrame([["potatoes",20],["carots",39], ["tomatos",40], ["apples",2], ["bananas",2]] , columns = ["aliments","number"])  

I would like to make a piechart where I group apples and bananas in a slice called vegetables.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I picked an arbitrary cutoff point of 20. You can take whatever cutoff point you want. This overwrites the apple and banana values with vegetable. Then sums them up using groupby. After that you can just use your regular pie chart code.

df= pd.DataFrame([["potatoes",20],["carots",39], ["tomatos",40], ["apples",2], ["bananas",2]] , columns = ["aliments","number"])  

df_draw = df.copy()
df_draw.loc[df_draw['number'] < 20, 'aliments'] = 'vegetables'

df_draw = df_draw.groupby('aliments')['number'].sum().reset_index()

plt.pie(df_draw['number'], labels=df_draw['aliments'], autopct='%.0f%%');

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading