Get values of a column having more than a certain value for given number of times(获取给定次数内具有超过特定值的列的值)
本文介绍了获取给定次数内具有超过特定值的列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据帧df
AS:
Election Year Votes Votes % Party Region
0 2000 42289 29.40 Janata Dal (United) A
1 2000 27618 19.20 Rashtriya Janata Dal B
2 2000 20886 14.50 Bahujan Samaj Party C
3 2000 17747 12.40 Congress D
4 2000 14047 9.80 Independent E
5 2005 8358 5.80 Janvadi Party A
6 2005 4428 13.10 Independent B
7 2005 1647 1.20 Independent C
8 2005 1610 11.10 Independent D
9 2005 1334 15.06 Nationalist Party E
10 2010 1114 0.80 Independent A
11 2010 1042 10.5 Bharatiya Janta Dal B
12 2010 835 0.60 Independent C
13 2010 14305 15.50 Independent D
14 2010 22211 17.70 Congress E
我需要找到其中3个或更多政党在每个";选举年获得10%以上投票权的";地区";。
我已将选举年份按升序排序,投票百分比按降序排序:
df1 = df.sort_values(['Election Year','Votes %'], ascending = (True, False))
然后我拿到了每个地区的前3名:
top_3 = df1.groupby(['Election Year', 'Region']).head(3).reset_index()
现在如何查看前3个地区每年是否有10%或更多的选票?
推荐答案
您在找这样的东西吗?
def election(df):
count = df['Votes %'].gt(10).sum()
regions = ','.join(df['Region'].where(df['Votes %'].gt(10),'None').tolist())
return pd.Series({'count':count,'regions':regions})
ndf = df.groupby(['Election Year','Party']).apply(election)
ndf = ndf.replace(['None,','None'],'',regex=True)
这篇关于获取给定次数内具有超过特定值的列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:获取给定次数内具有超过特定值的列的值


基础教程推荐
猜你喜欢
- 合并具有多索引的两个数据帧 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01