我有以下数据框:
CLUSTER | COLUMN_X
------------------
1 | yes
------------------
1 | no
------------------
2 | yes
------------------
2 | yes
------------------
3 | no
------------------
3 | no
我想遍历集群至少有一行以“ yes”作为值的行。像这样:
for row in (rows of the clusters for which there is a 'yes'):
df['OUTPUT'][row] = 'ok'
for row not in (rows of the clusters for which there is a 'yes') :
df['OUTPUT'][row] = 'not ok'
这是预期的输出:
CLUSTER | COLUMN_X | OUTPUT
-----------------------------
1 | yes | ok
-----------------------------
1 | no | ok
-----------------------------
2 | yes | ok
-----------------------------
2 | yes | ok
-----------------------------
3 | no | not ok
-----------------------------
3 | no | not ok
答案 0 :(得分:1)
我厌倦了像下面这样使用transform
和any
df.COLUMN_X.eq('yes').groupby(df.CLUSTER).transform('any').map({True:'Ok',False:'not ok'})
所以我们尝试一些新的isin
s=df.loc[df.COLUMN_X.eq('yes'),'CLUSTER']
df['OUTPUT']='Not Ok'
df.loc[df.CLUSTER.isin(s),'OUTPUT']='ok'
df
Out[1191]:
CLUSTER COLUMN_X OUTPUT
0 1 yes ok
1 1 no ok
2 2 yes ok
3 2 yes ok
4 3 no Not Ok
5 3 no Not Ok