我有这个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


大熊猫有可能吗?

最佳答案

这是找到既有律师又有法官的人的方法

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/

10-12 22:03