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
CodePudding user response:
You don't need np.copy, nor np.where. Use pandas' Series.mask instead
dataframe['field1'] = dataframe['field1'].where(dataframe['field1'].isnull(),
dataframe['field2'])
If you really want to use np.where the syntax is:
dataframe['field1'] = np.where(dataframe['field1'].isnull(), # condition
dataframe['field2'], # value if True
dataframe['field1']) # value if False
CodePudding user response:
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
