Signup/Sign In
PUBLISHED ON: MARCH 25, 2021

Pandas DataFrame pad() Method

In this tutorial, we will learn the Python pandas DataFrame.pad() method. This method is similar to the DataFrame.fillna() method and it fills NA/NaN values using the ffill() method.

It returns the DataFrame object with missing values filled or None if inplace=True.

The below shows the syntax of the DataFrame.pad() method.

Syntax

DataFrame.pad(axis=None, inplace=False, limit=None, downcast=None)

Parameters

axis: It represents index or column axis, '0' for index and '1' for the column. When the axis=0, method applied over the index axis and when the axis=1 method applied over the column axis

inplace: It represents bool(True or False), and the default value is False. If True, it fills in-place and returns None.

limit: int, default None. If the method is specified, this is the maximum number of consecutive NaN values to forward fill.

Example 1: Fill the missing values in pandas Dataframe

Here, by using the DataFrame.pad() method, we can fill all null values or missing values in the DataFrame. It fills the missing values by using the ffill method of pandas.

#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.pad(axis=0))


-----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 2: Fill the missing values in pandas Dataframe

This example is similar to the previous one, here DataFrame.pad() method fills the null values along the column axis.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[2, np.nan, np.nan],[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.pad(axis=1))


-----The DataFrame is-----
A B C
0 2.0 NaN NaN
1 NaN NaN 5.0
2 NaN 3.0 NaN
-----Filling Nan values------
A B C
0 2.0 2.0 2.0
1 NaN NaN 5.0
2 NaN 3.0 3.0

Example 2: Fill the missing values in pandas Dataframe with a limit in Pandas

We can replace the first NaN element using the limit method in the DataFrame.pad() method. It will make a limit to replace the elements in the dataframe.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
df = pd.DataFrame([[2, np.nan, np.nan],[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.pad(axis=1,limit=1))


-----The DataFrame is-----
A B C
0 2.0 NaN NaN
1 NaN NaN 5.0
2 NaN 3.0 NaN
-----Filling Nan values------
A B C
0 2.0 2.0 NaN
1 NaN NaN 5.0
2 NaN 3.0 3.0

Conclusion

In this tutorial, we learned the Python pandas DataFrame.pad() method. We learned the syntax, parameter and by applying this method on the DataFrame, we solved examples and understood the DataFrame.pad() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.