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 interpolate()
The interpolate()
method fills all NaN
values using interpolation.
The syntax for this method is as follows:
DataFrame.interpolate(method='linear', axis=0, limit=None, inplace=False, limit_direction=None, limit_area=None, downcast=None, **kwargs)
method | This parameter is the interpolation technique to use. The available options are: – linear : Ignore the index. Treat as spaced equally.– time : This parameter works on daily/high res to interpolate a specified time interval.– index , values : Use the numeric values of the index.– pad : Fill in any NaN values with existing values.– nearest/zero/slinear/quadratic/cubic/spline/barycentric/polynomial : Use the numeric values of the index. Polynomial and spline need an order (int ).– krogh/piecewise_polynomial/spline/pchip/akima/cubic/spline : Wraps around the SciPy Interpolation method(s) of similar name(s).– from_derivatives : Refers to scipy.interpolate.BPoly.from_derivatives which replaces βpiecewise_polynomial β interpolation method in Scipy 0.18. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
limit | The maximum number of successive NaN values to fill. Must be more than zero (0). |
inplace | If set to True, the changes apply to the original DataFrame/Series. If False, the changes apply to a new DataFrame/Series. By default, False. |
limit_direction | The successive NaN values fill in with the specified direction. – If limit : If method pad/ffill , set direction to forward. If method backfill/bfill , set direction to backward.– If no limit: If method backfill/bfill , the default direction is backward. Otherwise forward. |
The DataFrame in this example contains missing data. This code will attempt to replace these values.
df = pd.DataFrame({'Data-1': [np.nan, 11, 12], 'Data-2': [13, 14, pd.NaT], 'Data-3': [None, 15, 16]}, index=['Row-1', 'Row-2', 'Row-3']) print(df) result = df.interpolate(method='linear', limit_direction='backward', axis=0) print(result)
- Line [1] creates a dictionary of lists and saves it to
df
. - Line [2] outputs the DataFrame to the terminal.
- Line [3] interpolates and sets the parameters to
linear
, the direction to backward, and theaxis
to zero (0). This output saves to theresult
variable. - Line [4] outputs the result to the terminal.
Output
df
Data-1 | Data-2 | Data-3 | |
Row-1 | NaN | 13.0 | NaN |
Row-2 | 11.0 | 14.0 | 15.0 |
Row-3 | 12.0 | NaT | 16.0 |
result
Data-1 | Data-2 | Data-3 | |
Row-1 | 11.0 | 13.0 | 15.0 |
Row-2 | 11.0 | 14.0 | 15.0 |
Row-3 | 12.0 | NaT | 16.0 |
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.