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 :

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%%');

Leave a Reply