根据条件替换列中的值

阿努

我有DF

***********************
Product       Rate      Imputed_rate      min_rate

1              0        10                5
2              0        4                 7
3              0        34                2
4              0        3                 8
***************************

我想在Rate = 0时替换该行;仅当'Imputed_rate'<min_rate时才使用'min_rate'


做这个的最好方式是什么?


所需输出:


Product       Rate      Imputed_rate      min_rate

1             0         10                5
2             7         4                 7
3             0         34                2
4             8         3                 8
***************************
保罗·布伦南

np.where是你的朋友吗https://numpy.org/doc/stable/reference/generated/numpy.where.html

df['Rate'] = np.where(((df['Rate'] == 0) & (df['Imputed_rate'] < df['min_rate'])), df['min_rate'],df['Rate'])

对于熊猫中的行,基本上是if if else else。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章