chink
chink

Reputation: 1653

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

Upvotes: 0

Views: 650

Answers (2)

BENY
BENY

Reputation: 323366

I am using np.where

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

Upvotes: 0

lmiguelvargasf
lmiguelvargasf

Reputation: 69963

This should make the trick:

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

Upvotes: 2

Related Questions