[英]pandas apply function on column only if condition on another column is met
I have a dataframe:我有一个数据框:
df = A. Cond Val
1. True 0.8
5. False 0.8
2. False 0.6
4. False 0.5
I want to update the value of the columns 'Val' by truncate it in 0.1, only when Cond is False and val is higher than 0.55.我想通过在 0.1 中截断它来更新列“Val”的值,仅当 Cond 为 False 且 val 高于 0.55 时。 So new df will be:
所以新的 df 将是:
df = A. Cond Val
1. True 0.8
5. False 0.7
2. False 0.5
2. False 0.5
What is the best way to do it?最好的方法是什么?
Use boolean indexing
with DataFrame.loc
, for test False
values invert maks by ~
and chain another mask by Series.gt
:将
boolean indexing
与DataFrame.loc
一起使用,用于测试False
值通过~
反转 maks 并通过Series.gt
链接另一个掩码:
df.loc[df['Val'].gt(0.55) & ~df['Cond'], 'Val'] -= 0.1
print (df)
A. Cond Val
0 1.0 True 0.8
1 5.0 False 0.7
2 2.0 False 0.5
3 4.0 False 0.5
Use boolean indexing with two conditions and AND ( &
):使用带有两个条件和 AND (
&
) 的布尔索引:
df.loc[df['Val'].gt(0.55) & ~df['Cond'], 'Val'] -= 0.1
Output:输出:
A. Cond Val
0 1.0 True 0.8
1 5.0 False 0.7
2 2.0 False 0.5
3 4.0 False 0.5
Conditions:状况:
A. Cond Val df['Val'].gt(0.55) ~df['Cond'] AND
0 1.0 True 0.8 True False False
1 5.0 False 0.8 True True True
2 2.0 False 0.6 True True True
3 4.0 False 0.5 False True False
Using the int values of booleans, this would work too:使用布尔值的 int 值,这也可以:
df['val'] -= 0.1*(~df['cond'])*(df['val'] > 0.55)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.