gpt4 book ai didi

python - Pandas:复杂的聚合问题

转载 作者:行者123 更新时间:2023-12-01 04:24:59 25 4
gpt4 key购买 nike

我有这个Dataframe

In [4]: df = pd.DataFrame({'person':[1,1,2,2,3,3,3, 4], 'area':['A','B','A','A',
'C','C','C','C'], 'job type':['politics', 'expert', 'politics', 'politics', 'law
yer','judge', 'judge', 'lawyer']})

In [5]: df
Out[5]:
area job type person
0 A politics 1
1 B expert 1
2 A politics 2
3 A politics 2
4 C lawyer 3
5 C judge 3
6 C judge 3
7 C lawyer 4

我希望C区域的人能够查看他们是否既是律师又是法官,并将结果输出到第二个DataFrame,诸如此类喜欢

   person  jobtype
0 1 None
1 2 None
3 3 LAWYER_JUDGE
4 4 None

这对于 Pandas 来说可能吗?

最佳答案

这是寻找同时拥有律师和法官的人的一种方法

In [43]: zoomin = lambda x: None if len(np.setdiff1d(['judge', 'lawyer'], x['job type'])) else 'lawyer_judge'

In [44]: df.groupby('person').apply(zoomin)
Out[44]:
person
1 None
2 None
3 lawyer_judge
4 None
dtype: object

关于python - Pandas:复杂的聚合问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33257309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com