Ciaren

Coalesce columns

Coalesce columns — coalesceColumns

Take the first non-null value across several columns into a new column — a fallback chain for consolidating redundant fields.

Use cases

  • Pick the first available phone number: mobilehomework.
  • Merge values from differently-named source systems into one column.
  • Fall back to a default-bearing column when a primary is missing.

What it does

For each row, the columns are checked left-to-right and the first non-null value is written to the new column. If every column is null for a row, the result is null.

Configuration

Config keyTypeRequiredDescription
columnsstring[]YesColumns in priority order (≥ 2)
new_columnstringYesName of the result column
keep_originalbooleanNoKeep the source columns (default true)

Generated Python code

df_2 = df_1.assign(phone=lambda _d: _d['mobile'].where(pd.notna, _d['home']).where(pd.notna, _d['work']))

Tips & common mistakes

  • Priority is the column order — put the most-trusted source first.
  • To concatenate values instead of picking one, use Combine columns.

See also