Python 如何使用带有此数据的 Pandas 绘制饼图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31499033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:06:56  来源:igfitidea点击:

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

pythonpandas

提问by toy

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?

如何绘制饼图以显示有多少男性或女性吸烟?

采纳答案by Ami Tavory

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

在此处输入图片说明

回答by pcu

You can plot directly with pandas selecting piechart:

您可以直接使用 Pandas 选择pie图表进行绘图:

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

在此处输入图片说明

回答by OUMOUSS_ELMEHDI

here is a one liner :

这是一个单班轮:

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