Replace all NaN values with 0's in a column of Pandas dataframe


import modules

import pandas as pd
import numpy as np


create dummy dataframe

raw_data = {'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'],
'age': [20, 19, 22, 21],
'favorite_color': ['blue', 'red', 'yellow', "green"],
'grade': [88, 92, 95, 70]}
df = pd.DataFrame(raw_data, index = ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'])
df
age favorite_color grade name
Willard Morris 20 blue 88 Willard Morris
Al Jennings 19 red 92 Al Jennings
Omar Mullins 22 yellow 95 Omar Mullins
Spencer McDaniel 21 green 70 Spencer McDaniel


Create some NaN values in the dataframe

#First, we have to create the NaN values
df = df.replace(20,np.NaN)
df = df.replace(70,np.NaN)
df
age favorite_color grade name
Willard Morris NaN blue 88.0 Willard Morris
Al Jennings 19.0 red 92.0 Al Jennings
Omar Mullins 22.0 yellow 95.0 Omar Mullins
Spencer McDaniel 21.0 green NaN Spencer McDaniel


Replace the NaN values in the dataframe (with a 0 in this case)

#Now, we can replace them
df = df.fillna(0)
df
age favorite_color grade name
Willard Morris 0.0 blue 88.0 Willard Morris
Al Jennings 19.0 red 92.0 Al Jennings
Omar Mullins 22.0 yellow 95.0 Omar Mullins
Spencer McDaniel 21.0 green 0.0 Spencer McDaniel


Ace your next data science interview

Get better at data science interviews by solving a few questions per week



Find a bug? Submit a suggested change on Github, or message me on Twitter.