Pandas DataFrame fillna() Method
In this tutorial, we will learn the Python pandas DataFrame.fillna()
method. This method fills NA/NaN values using the specified method. It returns the DataFrame object with missing values filled or None
if inplace=True
.
The below shows the syntax of the DataFrame.fillna()
method.
Syntax
DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)
Parameters
value: scalar, dict, Series, or DataFrame.Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). Values not in the dict/Series/DataFrame will not be filled. This value cannot be a list.
method:{‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None. Method to use for filling holes in reindexed Series pad / ffill: propagate last valid observation forward to next valid backfill / bfill: use next valid observation to fill the gap.
axis:{0 or ‘index’, 1 or ‘columns’}. Axis along which to fill missing values.
inplace: bool, default False. If True, fill in-place. Note: this will modify any other views on this object (e.g., a no-copy slice for a column in a DataFrame).
limit: int, default None. If the method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If the method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None.
downcast: dict, default is None. A dict of item->dtype of what to downcast if possible, or the string ‘infer’ which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible).
Example 1: Replacing NaN Elements using the DataFrame.fillna()
Method
Here, by using the DataFrame.fillna()
metho,d we can replace all null values or missing values in the DataFrame with the specified value.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[2, np.nan, 0],[np.nan, np.nan,5],[np.nan,3,np.nan]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
print("-----Filling Nan values------")
print(df.fillna(2))
Once we run the program we will get the following output.
-----The DataFrame is-----
A B C
0 2.0 NaN 0.0
1 NaN NaN 5.0
2 NaN 3.0 NaN
-----Filling Nan values------
A B C
0 2.0 2.0 0.0
1 2.0 2.0 5.0
2 2.0 3.0 2.0
Example 2: Filling missing values using the DataFrame.ffill()
Method
By using the DataFrame.fillna()
method, we can also propagate non-null values forward with the ffill
method.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[2, np.nan, 0],[np.nan, np.nan,5],[np.nan,3,np.nan]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
print("-----Filling Nan values------")
print(df.fillna(method='ffill'))
Once we run the program, we will get the following output.
-----The DataFrame is-----
A B C
0 2.0 NaN 0.0
1 NaN NaN 5.0
2 NaN 3.0 NaN
-----Filling Nan values------
A B C
0 2.0 NaN 0.0
1 2.0 NaN 5.0
2 2.0 3.0 5.0
Example 3: Filling missing values using the DataFrame.ffill()
Method
Using the the DataFrame.fillna()
method, we can also propagate non-null values forward with the ffill
method along the axis=1.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[2, np.nan, 0],[np.nan, np.nan,5],[np.nan,3,np.nan]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
print("-----Filling Nan values------")
print(df.fillna(method='ffill',axis=1))
Once we run the program, we will get the following output.
-----The DataFrame is-----
A B C
0 2.0 NaN 0.0
1 NaN NaN 5.0
2 NaN 3.0 NaN
-----Filling Nan values------
A B C
0 2.0 2.0 0.0
1 NaN NaN 5.0
2 NaN 3.0 3.0
Example 4: Filling missing values using the DataFrame.ffill()
Method
Using the DataFrame.fillna()
method, we can also propagate non-null values backward with the bfill
method.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[2, np.nan, 0],[np.nan, np.nan,5],[np.nan,3,np.nan]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
print("-----Filling Nan values------")
print(df.fillna(method='bfill'))
Once we run the program we will get the following output.
-----The DataFrame is-----
A B C
0 2.0 NaN 0.0
1 NaN NaN 5.0
2 NaN 3.0 NaN
-----Filling Nan values------
A B C
0 2.0 3.0 0.0
1 NaN 3.0 5.0
2 NaN 3.0 NaN
Example 4: Filling missing values using the DataFrame.ffill()
Method
In the below example, the DataFrame.fillna()
method replaces all NaN elements in column ‘A’, ‘B’, ‘C’, and ‘D’, with 0, 1, 2, and 3 respectively.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[2, np.nan, 0],[np.nan, np.nan,5],[np.nan,3,np.nan]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
new_values = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
print("-----Filling Nan values------")
print(df.fillna(value=new_values))
Once we run the program, we will get the following output.
-----The DataFrame is-----
A B C
0 2.0 NaN 0.0
1 NaN NaN 5.0
2 NaN 3.0 NaN
-----Filling Nan values------
A B C
0 2.0 1.0 0.0
1 0.0 1.0 5.0
2 0.0 3.0 2.0
Example 5: Filling missing values using the DataFrame.ffill()
Method
We can replace the first NaN element using the limit
method in the DataFrame.fillna()
method.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[2, np.nan, 0],[np.nan, np.nan,5],[np.nan,3,np.nan]],columns=list('ABC'))
print("-----The DataFrame is-----")
print(df)
new_values = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
print("-----Filling Nan values------")
print(df.fillna(value=new_values,limit=1))
Once we run the program, we will get the following output.
-----The DataFrame is-----
A B C
0 2.0 NaN 0.0
1 NaN NaN 5.0
2 NaN 3.0 NaN
-----Filling Nan values------
A B C
0 2.0 1.0 0.0
1 0.0 NaN 5.0
2 NaN 3.0 2.0
Conclusion
In this tutorial, we learned the Python pandas DataFrame.fillna()
method. We learned the syntax, parameter and by applying this method on the DataFrame, we solved examples and understood the DataFrame.fillna()
method.