Fallback data completion
Populate incomplete records from a secondary DataFrame while preserving values already present in the primary frame.
Pandas · Module
Fills missing values in one DataFrame from corresponding non-missing values in another. The result uses the union of both DataFrames’ row and column indexes, while the first DataFrame takes precedence where both contain values.
Call combine_first with another DataFrame to create a new DataFrame that keeps the caller's non-missing cells and uses the other frame only for missing cells at corresponding row and column labels. The output expands to the union of both indexes and columns; a missing value remains where the other frame has no matching location or is also missing.
DataFrame.combine_first(other)| Name | Type | Description |
|---|---|---|
other | DataFrame | DataFrame used to supply values for missing cells. |
DataFrame containing the combined values from the two DataFrames.
import pandas as pd
primary = pd.DataFrame({"score": [10, None], "name": ["A", None]}, index=[1, 2])
fallback = pd.DataFrame({"score": [20, 30], "name": ["B", "C"]}, index=[2, 3])
result = primary.combine_first(fallback)
print(result)Missing cells in primary are filled from fallback at matching labels; the result also includes labels found only in either input.
Populate incomplete records from a secondary DataFrame while preserving values already present in the primary frame.
Merge frames with partially different row or column labels while retaining all labels.
Prefer a curated or current dataset and use an older or secondary dataset only for gaps.
It fills missing values in the calling DataFrame with non-missing values from the other DataFrame at the same labels. Values already present in the caller take precedence when both frames contain non-missing values.
Follow-up: What happens to the shape? The result uses the union of the two row indexes and column indexes, so it can contain rows or columns introduced by the other frame.
The argument is another DataFrame. It supplies candidate values for null cells in the caller, and the operation returns a DataFrame rather than modifying the caller in place.
Follow-up: What if the fallback has a value under a new label? That label is included in the returned frame because the indexes are combined by union.
Alignment is label-based: a fallback value is used only when its row and column labels correspond to a missing cell in the first DataFrame. Non-missing values in the first frame remain authoritative.
Follow-up: Can it fill every null? No. Missingness remains when the fallback lacks the location or also provides a missing value.
DataFrame.combineDataFrame.updateDataFrame.fillna