0

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      

2 Answers2

1

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'])
Joe
  • 12,057
  • 5
  • 39
  • 55
-1

try this:-

df[df['type']=='u']['Landsize'] = 0
Aniket Dixit
  • 152
  • 1
  • 5