1

I am trying to fill this 'C' column in such a way that when the value in 'A' is not NaN, 'C' takes value from 'B', else the value in 'C' remains unchanged.

Heres the code:

import pandas as pd
import numpy as np

df = pd.DataFrame({'A': ['greek', 'indian', np.nan, np.nan, 'australian'], 'B': 
     np.random.random(5)})
df['C'] = np.nan

df

I tried df.C = df.B.where(df.A != np.nan, np.nan), but it isnt working as the condition involves another column i think, for loop isnt yielding the desired result either. How to get there using shortest lines of codes as possible?

Raj Nair
  • 85
  • 1
  • 6

1 Answers1

0

The problem is not with np.where, the problem is that you are comparing the value directly against np.nan using !=

>>> np.nan == np.nan
False

So, use a function/method that allows you to check if the value is nan or not:

>>> df.C = df.B.where(df.A.notna(), np.nan)

            A         B         C
0       greek  0.030809  0.030809
1      indian  0.545261  0.545261
2         NaN  0.470802       NaN
3         NaN  0.716640       NaN
4  australian  0.148297  0.148297
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45