Examples of how to replace NaN values in a pandas dataframe
1 -- Create a dataframe
Lets consider the following dataframe:
import pandas as pdimport numpy as npdata = {'Name':['Ben','Anna','Zoe','Tom','John','Steve'],'Age':[20,27,43,30,np.nan,np.nan],'Gender':['M',np.nan,'F','M','M','M']}df = pd.DataFrame(data)
returns
Name Age Gender0 Ben 20.0 M1 Anna 27.0 NaN2 Zoe 43.0 F3 Tom 30.0 M4 John NaN M5 Steve NaN M
2 -- Replace all NaN values
To replace all NaN values in a dataframe, a solution is to use the function fillna(), illustration
df.fillna('',inplace=True)print(df)
returns
Name Age Gender0 Ben 20 M1 Anna 272 Zoe 43 F3 Tom 30 M4 John M5 Steve M
3 -- Replace NaN values for a given column
Example of how to replace NaN values for a given column ('Gender here')
df['Gender'].fillna('',inplace=True)print(df)
returns
Name Age Gender0 Ben 20.0 M1 Anna 27.02 Zoe 43.0 F3 Tom 30.0 M4 John NaN M5 Steve NaN M
4 -- Replace NaN using column type
Another example using the method dtypes:
df.dtypesName objectAge float64Gender objectdtype: object
to change NaNs based on column type:
for index, value in df.dtypes.items():if value == 'object':df[index] = df[index].fillna('')else:df[index] = df[index].fillna(0)print(df)
returns
Name Age Gender0 Ben 20.0 M1 Anna 27.02 Zoe 43.0 F3 Tom 30.0 M4 John 0.0 M5 Steve 0.0 M
