0

I'd like to modify dataframe when value in column = NaN. When I try this solution:

if df['columnx'].isnull().values.any():
    df['columnx'] = df['columnz']

all the columns seem to be converted, not only when meeting the criteria of NaN value.

This is what I get if I print the columnx values:

1  12345
2  12346
3  12347
4  NaN
5  NaN
6  NaN

When df['columnz'] has:

4   12355
5   12356
6   12357

I only want the the:

4   NaN
5   NaN
6   NaN

So I can convert

df['columnx'] = df['columnz']

So that I have df['columnx']:

1   12345
2   12346
3   12347
4   12355
5   12356
6   12357
anky
  • 74,114
  • 11
  • 41
  • 70
user270911
  • 91
  • 1
  • 13

1 Answers1

3

Using np.where

df['columnx'] = np.where(df['columnx'].isnull(),df['columnz'],df['columnx'])
BENY
  • 317,841
  • 20
  • 164
  • 234