How to replace NaN values in a pandas dataframe ?

How to replace NaN values in a pandas dataframe ?

Daidalos June 17, 2020


Examples of how to replace NaN values in a pandas dataframe

1 -- Create a dataframe

Lets consider the following dataframe:

import pandas as pd
import numpy as np

data = {'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 Gender
0    Ben  20.0      M
1   Anna  27.0    NaN
2    Zoe  43.0      F
3    Tom  30.0      M
4   John   NaN      M
5  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 Gender
0    Ben  20      M
1   Anna  27       
2    Zoe  43      F
3    Tom  30      M
4   John          M
5  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 Gender
0    Ben  20.0      M
1   Anna  27.0       
2    Zoe  43.0      F
3    Tom  30.0      M
4   John   NaN      M
5  Steve   NaN      M

4 -- Replace NaN using column type

Another example using the method dtypes:

df.dtypes

Name      object
Age       float64
Gender    object
dtype: 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 Gender
0    Ben  20.0      M
1   Anna  27.0       
2    Zoe  43.0      F
3    Tom  30.0      M
4   John   0.0      M
5  Steve   0.0      M

5 -- References