In this post, we are going to learn how to fill nan values of multiple columns in Pandas. The pandas dataframe fillna() method allow us to replace nan or missing value with the specified value. It takes 0 as an argument to replace the NAN values with zero and returns a new dataframe in which NAN values are replaced by zero. We can replace nan value with an empty string, blank, or custom value as needed. The dataframe replace() method is also used to replace NAN values it takes two arguments first is the original value and the Second, by which returns the original value is to be replaced.
1. Fillna() : fill nan values of all columns of Pandas
In this python program example, how to fill nan values of multiple columns by using fillna() method of pandas dataframe. We have multiple columns that have null values. The null/nan or missing value can add to the dataframe by using NumPy library np. nan attribute. To replace the multiple columns nan value. We have called fillna() method with the dataframe object. Let us understand with the below example.If you are looking to replace nan with zero or empty string follow this link.
import pandas as pd
import numpy as np
Student_dict = {
'Name': ['Jack', 'Rack', np.nan],
'Marks':[100.5,np.nan, np.nan],
'Subject': [np.nan, 'Math', 'Music']
}
dfobj = pd.DataFrame(Student_dict)
#whole dataframe
df = dfobj.fillna(0)
print (df)
Output
Name Marks Subject
0 Jack 100.5 0
1 Rack 0.0 Math
2 0 0.0 Music
2. Fillna(): Fill nan values of multiple columns in Pandas
In this python program code example, we will learn how to fill nan value in multiple columns using fillna() method of dataframe by specifying the columns name
dfobj = pd.DataFrame(Student_dict)
dfobj[['Name', 'Marks']] = dfobj[['Name', 'Marks']].fillna(0)
print (dfobj)
#output
Name Marks Subject
0 Jack 100.5 NaN
1 Rack 0.0 Math
2 0 0.0 Music
3. Fill nan values with unique value in each column
Sometimes it needs to fill the nan value of mutiple columns with a unique value in this code example we will discuss how to replace unique values of mutiple column in pandas dataframe using fillna() method. The argument inplace = true,we make changes in the existing dataframe object instead of returning the dataframe new object
dfobj.fillna({'Name':' ', 'Marks': 0, 'Subject': 'unknow'}, inplace=True)
print(dfobj)
Output
Name Marks Subject
0 Jack 100.5 unknow
1 Rack 0.0 Math
2 0.0 Music
4. Replace(): Fill nan of all columns of Pandas dataframe
In this python program. First, we have created a dataframe that has a null value in some columns. To add null value to the dataframe the numpy library np. nan attribute is used.
- The Python NumPy library is imported using “import numpy as np”.
- To replace mutiple column null value with zero,
- we have called replace() method with dataframe object and passed two arguments
- First, the value we want to replace that is np.nan
- Second, the value we want to replace with is 0.
import pandas as pd
import numpy as np
Student_dict = {
'Name': ['Jack', 'Rack', np.nan],
'Marks':[100.5,np.nan, np.nan],
'Subject': [np.nan, 'Math', 'Music']
}
dfobj = pd.DataFrame(Student_dict)
#whole dataframe
df = dfobj.replace(np.nan, 0)
print (df)
Output
Name Marks Subject
0 Jack 100.5 0
1 Rack 0.0 Math
2 0 0.0 Music
5. Replace() : Fill nan of multiple columns of Pandas Dataframe
In this python code example, we will discuss how to fill nan of specific mutiple columns of pandas dataframe using the replace() method of dataframe
dfobj[['Name', 'Marks']] = dfobj[['Name', 'Marks']].replace(np.nan, 0)
print (dfobj)
Output
Name Marks Subject
0 Jack 100.5 NaN
1 Rack 0.0 Math
2 0 0.0 Music
Summary
In this post, we have learned mutiple ways of how to fill nan values of multiple columns in Pandas data by using the dataframe method replace() and fillna() for entire dataframe or mutiple columns.