使用 python pandas 绘制特定列中数据的饼图

问题描述 投票:0回答:1

假设我有一个这样的 csv 数据,有几千行

| Src_Mac_address |Dst_Mac_address2| Src_Mac_country| 1 x1 y1 国家 1
2 x2 y2 国家 2 3 x3 y3 国家 3 4 x4 y4 国家 4 5 x5 y5 国家 5 6 x6 y6 国家 1 7 x7 y7 国家 1 8 x8 y8 国家 2

我想在 src_mac_country 列中找出每个国家/地区的频率,并想用该国家/地区的百分比份额绘制饼图。src_mac_country 列中有 30 多个国家,但我只想根据他们的排名绘制前 10 个国家/地区。发生次数(降序)

df=pd.read_csv('Filepath')
#df['Src_Mac_country'].value_counts()
#df.Src_Mac_country.value_counts(normalize=True).mul(100).round(1).astype(str)+'%'

这显示了每个国家在 Src_Mac_country 中出现了多少次 下一行显示该列的那个国家的百分比(即 Src_Mac_country) 但我想在饼图中绘制这些数据,我只想绘制前 10 个国家,根据它们出现的百分比(降序)。我该怎么做? 如果数据不清楚,请参考图片

python pandas dataframe
1个回答
0
投票

IIUC 用途:

N = 10
df.Src_Mac_country.value_counts(normalize=True).head(N).plot.pie()

或:

N = 10
s = df.Src_Mac_country.value_counts(normalize=True).head(N)
s.loc['other'] = 1 - s.sum()
s.plot.pie()
© www.soinside.com 2019 - 2024. All rights reserved.