Rate this post

## Preparation

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

• 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

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)

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
result

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