I need to replace NaN values with 0, but only when Type = u. How can I do it?
index Landsize Type
1 NaN u
2 NaN m
I need to replace NaN values with 0, but only when Type = u. How can I do it?
index Landsize Type
1 NaN u
2 NaN m
You can use:
df['Landsize'] = np.where(df['Type'] =='u', 0, np.nan)
or if not all values in df['Landsize']
are nan
:
df['Landsize'] = np.where(df['Type'] =='u', 0, df['Landsize'])
try this:-
df[df['type']=='u']['Landsize'] = 0