Pandas DataFrame: reindex() function
DataFrame - reindex() function
The reindex() function is used to conform DataFrame to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index.
Syntax:
DataFrame.reindex(self, labels=None, index=None, columns=None, axis=None, method=None, copy=True, level=None, fill_value=nan, limit=None, tolerance=None)
Conform DataFrame to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False.
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
labels | New labels / index to conform the axis specified by ‘axis’ to. |
array-like | Optional |
index, columns | New labels / index to conform to, should be specified using keywords. Preferably an Index object to avoid duplicating data | array-like | Optional |
axis | Axis to target. Can be either the axis name (‘index’, ‘columns’) or number (0, 1). | int or str, | Optional |
method | Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index.
|
{None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’} | Required |
copy | Return a new object, even if the passed indexes are the same. | bool Default Value: True |
Required |
level | Broadcast across a level, matching Index values on the passed MultiIndex level. | int or name | Required |
fill_value | Value to use for missing values. Defaults to NaN, but can be any “compatible” value. | scalar Default Value: np.NaN |
Required |
limit | Maximum number of consecutive elements to forward or backward fill. | int Default Value: None |
Required |
tolerance | Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations most satisfy the equation abs(index[indexer] - target) <= tolerance. Tolerance may be a scalar value, which applies the same tolerance to all values, or list-like, which applies variable tolerance per element. List-like includes list, tuple, array, Series, and must be the same size as the index and its dtype must exactly match the index’s type. |
Optional |
Returns: DataFrame with changed index.
Example:
Download the Pandas DataFrame Notebooks from here.