fill in missing data with nan in python

In pandas, missing or null values are represented by NaN. To fill in missing data with NaN in python using pandas, you can use the fillna method.

main.py
import pandas as pd

# create a dataframe
data = {'name':['John', 'Mary', 'Peter', 'James', 'David'],
        'age':[35, 27, 23, 47, 28],
        'gender':['M', 'F', 'M', 'M', 'M'],
        'score':[56, 78, None, 89, None]}
df = pd.DataFrame(data)

# fill in missing data with NaN
df.fillna(value=pd.np.nan, inplace=True)
322 chars
12 lines

In the above example code, the fillna() function takes an argument value, which is set to pd.np.nan to fill in missing values with NaN. The inplace parameter is used to modify the dataframe in place i.e it returns None and modifies the original dataframe by setting the missing data as NaN.

related categories

gistlibby LogSnag