Reputation: 12161
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
Reputation: 519
here is a one liner :
temp[temp.Smoke==1]['Sex'].value_counts().plot.pie()
Upvotes: 9
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()
Upvotes: 10
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()
Upvotes: 19