This tutorial will discuss about unique ways to replace nan values in a column in pandas.
Table Of Contents
Preparing DataSet
Create a DataFrame with certain rows and columns.
Let’s see the complete example,
import pandas as pd import numpy as np # List of Tuples employees= [('Mark', 'US', 'Tech', 5), ('Riti', np.NaN, 'Tech' , 7), ('Shanky', np.NaN, 'PMO' , 2), ('Shreya', 'UK', 'Design', 2), ('Aadi', np.NaN, 'Tech', 11), ('Sim', 'US', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Location', 'Team', 'Experience']) print(df)
Output
Name Location Team Experience 0 Mark US Tech 5 1 Riti NaN Tech 7 2 Shanky NaN PMO 2 3 Shreya UK Design 2 4 Aadi NaN Tech 11 5 Sim US Tech 4
Replace NaN values using fillna()
Select the column, and call fillna() method on it. It accepts a replacement value as first argument, and replaces all the NaN values in the column with that replacement value. Also pass the inplace parameter as True, so that it changes the column in place. Let’s see an example,
Here we will replace all the NaN values in column Location
with default value Australia
.
Frequently Asked:
# Replace all NaN values in column 'Location' # with string 'Australia' df['Location'].fillna('Australia', inplace=True) print(df)
Output:
Name Location Team Experience 0 Mark US Tech 5 1 Riti Australia Tech 7 2 Shanky Australia PMO 2 3 Shreya UK Design 2 4 Aadi Australia Tech 11 5 Sim US Tech 4
Replace NaN values replace fillna()
Select the column, and call replace() method on it. Pass the value to be replaced i.e. np.NaN
, and replacement string i.e. Australia
as parameters in it. Also, pass the inplace parameter as True in it, so that all modifications are done in place in the column.
Here it will replace all the NaN values in column Location
with default value Australia
.
# Replace all NaN values in column 'Location' # with string 'Australia' df['Location'].replace(np.nan, 'Australia', inplace=True) print(df)
Output:
Name Location Team Experience 0 Mark US Tech 5 1 Riti Australia Tech 7 2 Shanky Australia PMO 2 3 Shreya UK Design 2 4 Aadi Australia Tech 11 5 Sim US Tech 4
Summary
We learned about different ways to replace NaN values in DataFrame column with given value.