简体   繁体   中英

Fill null values based on the values of the other column of a pandas dataframe

I want to fill the null values of a column based on the values of other column. I want to fill it as 0 if other column is 0 or else leave it as null.

A  B   C
1  1   0
0 NAN  2
2 NAN  0

I want the result as

A   B   C
1   1   0
0  NAN  2
2   0   0

这应该可以解决问题:

df['B'] = np.where(df['C']== 0, 0, np.nan)

我正在使用np.where

df['B']=np.where(df.B.isnull()&df.C.eq(0),0,df.B)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM