我有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
大熊猫有可能吗?
答案 0 :(得分:2)
这是找到同时拥有律师和法官的人的一种方式
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