Pandas: 根据一列的条件来替换另一列的值
dataframe是这样的
df.head(10)
RT (min) Area (Ab*s) Quality patch similarity
8 10.167 23278313 64 NaN NaN
9 10.167 23278313 47 NaN NaN
10 10.167 23278313 38 NaN NaN
28 10.333 3407159 49 10.167 0.983935
29 10.333 3407159 22 10.167 0.983935
30 10.333 3407159 16 10.167 0.983935
48 10.390 3299202 38 10.333 0.994514
49 10.390 3299202 35 10.333 0.994514
50 10.390 3299202 32 10.333 0.994514
68 10.516 2015786 50 10.390 0.988018
当 df['similarity']>0.99
的时候,df['RT(min)'] = df['patch']
,例如处理后的结果应该是这样的
RT (min) Area (Ab*s) Quality patch similarity
8 10.167 23278313 64 NaN NaN
9 10.167 23278313 47 NaN NaN
10 10.167 23278313 38 NaN NaN
28 10.333 3407159 49 10.167 0.983935
29 10.333 3407159 22 10.167 0.983935
30 10.333 3407159 16 10.167 0.983935
48 10.333 3299202 38 10.333 0.994514
49 10.333 3299202 35 10.333 0.994514
50 10.333 3299202 32 10.333 0.994514
68 10.516 2015786 50 10.390 0.988018
48,49,50行的RT(min) 被相应的patch值给取代了
解决方法
参数解释: Where cond is False, keep the original value. Where True, replace with corresponding value fromother. 也就是说,只有条件是true的 ,参会返回value
df['RT'] = df['RT'].mask(df['similarity'] > 0.99, df['patch'])
网友评论