Smbat
Smbat

Reputation: 71

Python pie chart / Show several columns combined

I have dataframe with 2 columns:

Col1- managers' name

Col2 - their profit

I want plot a pie chart where I can show most profitable 5 managers seperately , and others in one slice as 'others'

Upvotes: 0

Views: 848

Answers (1)

Felix Z.
Felix Z.

Reputation: 317

How about that: With automatic labeling of the pie pieces using autopct argument.

import pandas as pd
import matplotlib.pyplot as plt

data  = {'managers':['mike1','mike2','mike3','mike4','mike5','mike6','mike7'],
'profit':[110,60,40,30,10,5,5],
}

df = pd.DataFrame(data)

df = df.sort_values(by = 'profit', ascending = False)

top_5 = df.iloc[:5] 

others = df.iloc[5:]['profit'].sum()

df2 = pd.DataFrame([['others',others]], columns = ['managers','profit'])

all_data = top_5.append(df2, ignore_index=True)

all_data.index = all_data['managers']

#func to lable the pieces
def auto_func(val):
    return(round(val)) 

all_data.plot.pie(y = 'profit', autopct = auto_func)

# ax = plt.gca()

plt.show()

Upvotes: 1

Related Questions