python - Pandas Python - 对其他人进行分组计数
问题描述
我正在使用 python 和 pandas 对一个项目进行数据分析,其中我有以下数据:
数字是计数。
USA: 5000
Canada: 7000
UK: 6000
France: 6500
Spain: 4000
Japan: 5
China: 7
Hong Kong: 10
Taiwan: 6
New Zealand: 8
South Africa: 11
我的任务是制作一个代表计数的饼图。
df['Country'].value_counts().plot.pie()
我将得到一个饼图,但我想将计数较小的国家/地区组合起来,并将它们归入与其他类似的类别。
我怎样才能做到这一点?
解决方案
IIUC 使用np.where
设置边界,然后groupby
+ sum
,注意我在这里使用pandas.Series.groupby
s=df['Country'].value_counts()
s.groupby(np.where(s>=4000,s.index,'other')).sum()#.plot.pie()
Out[64]:
Canada 7000
France 6500
Spain 4000
UK 6000
USA 5000
other 47
推荐阅读
- excel - 如何根据单元格中的值复制行
- gitlab - GitLab Pages - 在现有项目上激活
- python - 如何将默认值设置为自动生成的 OneToOneField 关系?
- python - 如何在 AWS Glue 中使用 Crypto.Cipher AES?
- omnet++ - Castalia S-MAC 参数
- java - 我们使用后期绑定的实际场景有哪些?
- javascript - 在子组件加载之前加载不是子组件。页面闪烁(lazyLoading+Angular Universal)
- angular - 在材质表中使用角度键值管道显示数组对象
- java - Button中的按钮(类)不能应用于类
- c# - 以编程方式填充 XAML ListView ItemsSource