蟒:如果列条件在该列中遇到变化值重复]

问题描述 投票:0回答:1

如何添加10到col2列任何价值,如果它的值为0。更大的数据帧中的一般情况。

     col1 col2
row1    1   2
row2    3   0

所以,我得到:

     col1 col2
row1    1   2
row2    3   10

这让我正确的行:

df2.loc[lambda df2: df2['col2'] == 0 ] 

这确实整行,我不想要的:

df2.loc[df2['col2'] == 0 ] = 10
python python-3.x
1个回答
1
投票

使用np.where()

import numpy as np

df['col2'] = np.where(df['col2']==0, 10, df['col2'])
© www.soinside.com 2019 - 2024. All rights reserved.