Data alignment
Align rows from multiple tables before arithmetic or concatenation.
Pandas · Module
Reindex a DataFrame to specified row and/or column labels, optionally filling newly introduced gaps. Missing labels become NaN by default or receive a supplied fill value.
DataFrame.reindex creates a DataFrame aligned to requested row and/or column labels. Existing labels are reordered, absent labels are added with missing values or a chosen fill value, and optional methods can fill gaps when the index supports them.
DataFrame.reindex(*labels=None, **, index=None, columns=None, axis=None, method=None, copy=<no_default>, level=None, fill_value=nan, limit=None, tolerance=None)| Name | Type | Description |
|---|---|---|
labels | array-like, optional | Target labels for the selected axis. |
index | array-like, optional | Target row labels. |
columns | array-like, optional | Target column labels. |
axis | int or str, optional | Axis to reindex: 'index', 'columns', 0, or 1. |
method | {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'} | Gap-filling strategy: None, backfill/bfill, pad/ffill, or nearest. |
copy | bool or no_default | Whether to copy data; the current stable API uses a no-default sentinel. |
level | int or level name, optional | Broadcast target labels across a level for a MultiIndex. |
fill_value | scalar, default NaN | Value used for newly introduced labels. |
limit | int, optional | Maximum consecutive labels to fill. |
tolerance | scalar or list-like, optional | Maximum distance for inexact matching. |
DataFrame with changed index.
import pandas as pd
frame = pd.DataFrame({"value": [10, 20]}, index=["a", "b"])
result = frame.reindex(index=["b", "c"], fill_value=0)
print(result)Reorders the existing row and adds the missing row with the specified fill value.
Align rows from multiple tables before arithmetic or concatenation.
Ensure a DataFrame has a required row or column schema.
Create regularly ordered labels and fill gaps from nearby observations.
It conforms a DataFrame to requested labels on one or both axes. Existing labels are reordered, while labels absent from the original index receive missing values unless filling is requested.
Follow-up: How would you add columns rather than rows? Pass the desired labels with the columns parameter, or target the columns axis through labels and axis.
Use index and columns for explicit axis selection, or labels with axis for the alternate calling convention. The method returns a DataFrame with the changed index or columns.
Follow-up: Why prefer index and columns in production code? They make the target axis explicit and allow both axes to be changed in one call.
method controls filling of holes and accepts None, backfill/bfill, pad/ffill, or nearest. It is intended for ordered indexes; limit and tolerance further restrict filling behavior.
Follow-up: When is tolerance useful? It limits how far a nearest-style match may search, with scalar or per-element values supported by the documented API.
DataFrame.reindex_likeSeries.reindex