Skip to content
Advertisement

Begginer/ numpy where and copy

I am trying to copy values from one Field2 into Field1 if Field1 is null or NaN. I have tried below where statement as per documentation, but it cuts outliners instead of copyting the value.

dataframe=np.where(dataframe['field1'].isnull(),np.copy(dataframe['field2']),1)

I have interpreted it as if statement, but apparently its wrong interpretation, as results are not correct. Has anyone of you had similar issues?

np.where source np.copy source

Advertisement

Answer

Use fillna or combine_first:

dataframe['field1'] = dataframe['field1'].fillna(dataframe['field2'])

# OR

dataframe['field2'] = dataframe['fields'].combine_first(dataframe['field2'])

Demo:

dataframe = pd.DataFrame({'field1': [1, np.NaN, 3], 'field2': [4, 5, 6]})
print(dataframe)

# Output
   field1  field2
0     1.0       4
1     NaN       5
2     3.0       6


###
dataframe['field1'] = dataframe['field1'].fillna(df['field2'])
print(dataframe)

# Output
   field1  field2
0     1.0       4
1     5.0       5
2     3.0       6
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement