Pandas DataFrame pad() Method


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame pad()

The pad() method is an alias for DataFrame/Series fillna() with the parameter method set to 'ffill'.


DataFrame fillna()

The fillna() method fills in the DataFrame/Series missing data (NaN/None) with the content of the value parameter is shown below.

The syntax for this method is as follows:

Frame.fillna(value=None, method=None, axis=None, 
             inplace=False, limit=None, downcast=None)
valueThis value is a value to fill in the missing values. This value can be a single value or a dictionary for a value-for-value replacement. Anything not in the dictionary remains unchanged.
methodThe method to use to fill in the missing values. The choices are: pad/ffill: complete with last value. backfill/bfill: complete with next value.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
inplaceIf set to True, the changes apply to the original DataFrame/Series. If False, the changes apply to a new DataFrame/Series. By default, False.
limitThe maximum number of elements to backward/forward fill.
downcastThe only available selection is the infer option. This attempts to convert floats (float64) to integers (int64).

In this example, the DataFrame contains some missing data. This code will attempt to (replace) these values using the fillna() method.

df = pd.DataFrame({'Data-1':  [np.nan, 11, 12], 
                   'Data-2':  [13, 14, np.nan],
                   'Data-3':  [np.nan, 15, 16]},
                   index=['Row-1', 'Row-2', 'Row-3'])
print(df)

result = df.fillna(22, downcast='infer')
print(result)
  • Line [1] creates a dictionary of lists and saves it to df.
  • Line [2] outputs the DataFrame to the terminal. All np.nan values convert to NaN.
  • Line [3] fills in the missing values across the rows with the value 22. The infer parameter attempts to change the dtype across the DataFrame/Series. This output saves to the result variable.
  • Line [4] outputs the result to the terminal.

Output

df
 Data-1 Data-2 Data-3
Row-1    NaN13.0NaN
Row-2   11.014.015.0
Row-3   12.0NaN16.0
result
 Data-1 Data-2 Data-3
Row-1    221322
Row-2   111415
Row-3   122216

πŸ’‘ Note: The output using ffill() is the same as if you use fillna() and pass the method parameter as ffill.

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.