Combine Columns with Fallback Values
Choose a primary email when it exists, otherwise use a backup email. Coalesce checks columns in order and copies the first non-null value into a new column.
You'll use: File Input → Coalesce Columns → File Output.
Steps
- Upload your CSV on Datasets and select it in File Input.
- Add Coalesce Columns with
columns: ["primary_email", "backup_email"]andnew_column: email. - Leave
keep_original: trueto compare the source and result columns. - Preview the node. A row with two missing values should still have null
in
email. - Connect File Output, choose CSV, and run the flow.
Before / after
| primary_email | backup_email | |
|---|---|---|
[email protected] | [email protected] | [email protected] |
| null | [email protected] | [email protected] |
| null | null | null |
| empty string | [email protected] | empty string |
Exported Python
These runnable examples use the same configuration. The transformation lines come from Ciaren's code generators.
pandas
python
import pandas as pd
df_1 = pd.DataFrame({
'primary_email': ['[email protected]', None, None, ''],
'backup_email': ['[email protected]', '[email protected]', None, '[email protected]'],
})
df_2 = df_1.assign(email=lambda _d: _d['primary_email'].where(pd.notna, _d['backup_email']))
print(df_2)Polars
python
import polars as pl
df_1 = pl.DataFrame({
'primary_email': ['[email protected]', None, None, ''],
'backup_email': ['[email protected]', '[email protected]', None, '[email protected]'],
})
df_2 = df_1.with_columns(pl.coalesce([pl.col('primary_email'), pl.col('backup_email')]).alias('email'))
print(df_2)Tips
- Put the most trusted column first. Coalesce does not compare or merge two non-null values.
- An empty string is not null, so it takes priority over the backup. If empty text means missing in your source, normalize it to null first.
- Set
keep_original: falseonly after checking the output, to drop the source columns. - To join pieces of text together, use Combine Columns.