Group small values in a pie chart
Question:
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.
Answers:
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%%');
Please refer to the link where I use a conditional selection for a similar case.
The counts sum of all small contributors to the pie chart is appended to the dataframe and given for plotting
GitHub Link
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.
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%%');
Please refer to the link where I use a conditional selection for a similar case.
The counts sum of all small contributors to the pie chart is appended to the dataframe and given for plotting
GitHub Link