我有一个具有以下结构的数据框,
case when sum( case when MessageId= 1 then 1 else 0 end) =1
then 0 else
count(DISTINCT MessageId) end as cnt
最终结果必须是
master_mac slave_mac uuid rawData
0 ac233fc01403 ac233f26492b e2c56db5 NaN
1 ac233fc01403 ac233f26492b e2c56db5 NaN
2 ac233fc01403 ac233f26492b e2c56db5 NaN
3 ac233fc01403 ac233f26492b e2c56db5 ac0228
4 ac233fc01403 e464eecba5eb NaN 590080
5 ac233fc01403 ac233f26492b e2c56db5 ac0228
6 ac233fc01403 ac233f26492b e2c56db5 NaN
7 ac233fc01403 ac233f26492b e2c56db5 636800
有人可以帮我吗?
答案 0 :(得分:2)
使用:
m = df['uuid'].notna()
如果每个组需要处理,请使用GroupBy.transform
和GroupBy.any
来测试每个组至少一个非NaN
:
m = df['uuid'].notna().groupby([df['master_mac'],df['slave_mac']]).transform('any')
df['rawData'] = df['rawData'].mask(m)
print (df)
master_mac slave_mac uuid rawData
0 ac233fc01403 ac233f26492b e2c56db5 NaN
1 ac233fc01403 ac233f26492b e2c56db5 NaN
2 ac233fc01403 ac233f26492b e2c56db5 NaN
3 ac233fc01403 ac233f26492b e2c56db5 NaN
4 ac233fc01403 e464eecba5eb NaN 590080
5 ac233fc01403 ac233f26492b e2c56db5 NaN
6 ac233fc01403 ac233f26492b e2c56db5 NaN
7 ac233fc01403 ac233f26492b e2c56db5 NaN
或者:
df.loc[m, 'rawData'] = np.nan
答案 1 :(得分:0)
如果您需要根据rawData
列中的值为每一行修改uuid
列中的值,只需执行以下操作:
df['rawData'].loc[df['uuid'].notna()] = np.nan