toy
toy

Reputation: 12161

How do I plot a pie chart using Pandas with this data

I have a data like this

ID   Sex   Smoke
1  female    1 
2    male    0
3   female   1

How do I plot a pie chart to show how many male or female smokes?

Upvotes: 13

Views: 47264

Answers (3)

OUMOUSS_ELMEHDI
OUMOUSS_ELMEHDI

Reputation: 519

here is a one liner :

temp[temp.Smoke==1]['Sex'].value_counts().plot.pie()

Upvotes: 9

pcu
pcu

Reputation: 1214

You can plot directly with pandas selecting pie chart:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'Sex': ['female', 'male', 'female'], 'Smoke': [1, 3, 1]})

df.Smoke.groupby(df.Sex).sum().plot(kind='pie')
plt.axis('equal')
plt.show()

enter image description here

Upvotes: 10

Ami Tavory
Ami Tavory

Reputation: 76396

Say you start with:

import pandas as pd
from matplotlib.pyplot import pie, axis, show

df = pd.DataFrame({
    'Sex': ['female', 'male', 'female'],
    'Smoke': [1, 1, 1]})

You can always do something like this:

sums = df.Smoke.groupby(df.Sex).sum()
axis('equal');
pie(sums, labels=sums.index);
show()

enter image description here

Upvotes: 19

Related Questions