Record R2 has no primary reading, but its backup contains 7. Record R1 already has a primary reading of zero. A rule that treats zero as empty would overwrite an observed value.

Use Series.combine_first() to select the primary value when it is present and the backup when the primary is missing. Keep both input columns and add a source label so the result shows where each selected reading came from.

Four records use primary readings when present: R1 keeps zero and R3 keeps 12. R2 takes 7 from Backup, while R4 has no value in either source and remains missing.
Primary readings take precedence. The backup supplies R2 with 7; R4 remains missing because both sources are missing. These readings are fictional.Credit: MathBlog

Choose the source before filling

These four fictional records contain whole-number readings. Keep a primary reading whenever one exists. A backup is another observed value, not an estimate of a missing value. That preference needs to make sense for your data; pandas cannot decide which source is more trustworthy.

Fictional readings before source selection
Record Primary Backup
R1 0 8
R2 Missing 7
R3 12 15
R4 Missing Missing

Keep the original columns and record the source

Run the code blocks in order with pandas installed. The dataset is included here. The capitalized Int64 dtype stores whole numbers together with pandas’ missing marker, pd.NA.

import pandas as pd

readings = pd.DataFrame({
    "record": ["R1", "R2", "R3", "R4"],
    "primary": pd.Series([0, None, 12, None], dtype="Int64"),
    "backup": pd.Series([8, 7, 15, None], dtype="Int64"),
})

result = readings.copy()
result["selected"] = result["primary"].combine_first(result["backup"])
result["source"] = "Missing"
result.loc[result["backup"].notna(), "source"] = "Backup"
result.loc[result["primary"].notna(), "source"] = "Primary"

print(result.to_string(index=False))

Expected output:

record  primary  backup  selected  source
    R1        0       8         0 Primary
    R2     <NA>       7         7  Backup
    R3       12      15        12 Primary
    R4     <NA>    <NA>      <NA> Missing

The source labels are assigned in order: first Missing, then Backup wherever a backup exists, and finally Primary wherever a primary exists. The final assignment gives Primary priority when both sources contain a value.

R1 keeps zero, R2 receives 7, and R3 keeps 12. R4 remains missing because neither source supplies a reading. The selected column also retains the nullable Int64 dtype in this example. The two source columns remain unchanged.

Review conflicting readings

Filling gaps does not resolve disagreement between two known values. R1 has zero in Primary and 8 in Backup; R3 has 12 and 15. The preference rule selects a value, but those discrepancies may still need investigation.

conflict = (
    readings["primary"].notna()
    & readings["backup"].notna()
    & readings["primary"].ne(readings["backup"])
)
print(readings.loc[conflict, ["record", "primary", "backup"]].to_string(index=False))

Expected output:

record  primary  backup
    R1        0       8
    R3       12      15

The two notna() checks restrict this review to records where both sources have values. A missing reading stays out of the disagreement list.

Match index labels, not row positions

The two columns in readings share one DataFrame’s index. Independently prepared Series need more care: use record IDs as index labels and check that those IDs are unique in each source.

primary = pd.Series([None, 0], index=["R2", "R1"], dtype="Int64")
backup = pd.Series([8, 7, 3], index=["R1", "R2", "R5"], dtype="Int64")

if not primary.index.is_unique or not backup.index.is_unique:
    raise ValueError("Use a unique record ID in each source.")

print("combine_first:")
print(primary.combine_first(backup).to_string())
print("fillna:")
print(primary.fillna(backup).to_string())

Expected output:

combine_first:
R1    0
R2    7
R5    3
fillna:
R2    7
R1    0

R2 receives its own backup of 7 even though the two Series have different row orders. combine_first() uses the union of the indexes, so the standalone result also contains R5. fillna(backup) fills by matching labels while keeping the primary Series’ index and order.

Assigning that union Series as a new column in an existing DataFrame aligns it to the DataFrame’s rows; it does not automatically add the extra R5 row. If you need to compare two full exports before choosing a preference, see comparing reports by record ID.

Decide what counts as missing

An empty string is present text to pandas. So is the literal string "NA" in this directly constructed Series. Only normalize blanks if your source definition says that blank text means missing.

primary_owner = pd.Series(["", None, "NA"], dtype="string")
backup_owner = pd.Series(["Ava", "Milo", "Nia"], dtype="string")

print(primary_owner.combine_first(backup_owner).tolist())
blank_as_missing = primary_owner.str.strip().replace("", pd.NA)
print(blank_as_missing.combine_first(backup_owner).tolist())

Expected output:

['', 'Milo', 'NA']
['Ava', 'Milo', 'NA']

The first result retains the empty string. The second strips surrounding whitespace, replaces an empty result with pd.NA, and can then use Ava as the fallback. Both retain the literal label "NA". Import readers can recognize additional missing-value codes, so check the import settings too. The guide to finding and filling missing values in pandas explains that cleanup step in more detail.

Check the dtype before using the result

The integer dtype is preserved here because both numeric sources use Int64 and contain compatible whole numbers. Combining different dtypes can change the result’s dtype. A valid fractional reading needs a type that can hold fractions, such as nullable Float64.

combine_first() can return a mixed-type Series if a backup contains text. Validate the source values and units before selecting between them. Inspect the remaining missing values and the source labels before calculating with the selected column.

These examples were executed with Python 3.11.3 and pandas 2.3.3.

Further reading