Ciaren

Fill nulls

Fill nulls — fillNulls

Replace missing values using a strategy.

Use cases

  • Backfill a constant like "Unknown" for a missing category.
  • Impute a numeric column with its mean/median so rows aren't lost.
  • Carry the last known value forward (ffill) in a time series.

What it does

Replaces nulls in the target columns without removing rows — unlike Drop nulls, every row survives.

Before
order_idregionamount
1001North120.5
1002null89
1003Southnull
1004null42.25
4 rows · 3 cols
Fill nulls (strategy=constant, value=Unknown, columns=[region])
After
order_idregionamount
1001North120.5
1002Unknown89
1003Southnull
1004Unknown42.25
4 rows · 3 cols

Configuration

Config keyTypeRequiredDescription
strategystringNoconstant (default), mean, median, mode, min, max, zero, ffill, bfill
valueanyConditionalRequired when strategy is constant
columnsstring[]NoLimit to these columns (otherwise all)

The mean, median, min, and max strategies are computed per column from the non-null values; mode uses the most frequent value; ffill/bfill propagate the previous/next value.

Generated Python code

# strategy: "constant", value: "Unknown", columns: ["region"]
df_2 = df_1.fillna({'region': 'Unknown'})

Tips & common mistakes

  • A value is only needed for constant. Computed strategies (mean, median, …) ignore it.
  • mean/median need numeric columns. Restrict columns so the strategy only touches columns it applies to.

See also