Skip to content

Fill Missing Values

A survey export has empty cells in age and region, and you want to keep every respondent instead of dropping rows. Use one Fill Nulls node per column group: the median for age, the text Unknown for region.

You'll use: File Input → Fill Nulls → Fill Nulls → File Output.

  1. 1Input
    File Input
    survey.csv
  2. 2Clean
    Fill Nulls
    age → median
  3. 3Clean
    Fill Nulls
    region → Unknown
  4. 4Output
    File Output
    survey_filled

Sample data

Save this as survey.csv. Respondent 2 has no age and respondent 3 has no region. An empty cell in a CSV is read as null.

csv
respondent_id,age,region
1,25,North
2,,South
3,40,
4,31,East
5,95,North

Steps

  1. On Datasets, upload survey.csv.
  2. In a new flow, add File Input. Set File type to CSV and Dataset to survey.csv.
  3. Add Fill Nulls. Set Strategy to Median and Columns (optional) to age.
  4. Add a second Fill Nulls. Set Strategy to Constant value, Fill value to Unknown, and Columns (optional) to region. Each node has one strategy, so a second column that needs a different fill needs a second node.
  5. Add File Output. Set File type to CSV (.csv) and Dataset name to survey_filled. Connect the nodes in order and click Run.

Prefer to describe it?

With a provider key set up, upload the sample data, open a blank flow in the same project and ask the Flow assistant, for example: "fill missing numbers with the column median and missing text with Unknown." Review the proposed nodes and settings, then choose Apply and save. We ran this prompt on the sample data; see what the assistant built. Its flow can differ from the steps above in node order and names, so check each setting. The assistant never runs a flow, so click Run yourself.

Result

respondent_idageregion
125.0North
235.5South
340.0Unknown
431.0East
595.0North

The median of 25, 31, 40 and 95 is 35.5. The filled value is a decimal, so the whole age column is written with decimals.

Choose the strategy

The age column has one null and one outlier, 95: the mean fills 47.75, the median fills 35.5

  • Median is the safer default for numbers with outliers. On this sample the mean would fill 47.75, older than three of the four known ages.
  • Mean (average), Minimum, Maximum and Zero also fill numeric columns. Most frequent (mode) works for text too.
  • Forward fill copies the value from the row above, Backward fill from the row below. Use them only on data in a meaningful order, such as a time series sorted by date. On this sample, forward fill would give respondent 3 the region South from respondent 2.

Check the result

Save the flow, click Preview, select the second Fill Nulls node and click Run preview. Respondent 2 should show age 35.5 and respondent 3 region Unknown. Switch to Profile: the null count for age and region should be 0.

Pitfalls

  • Set the columns. With Columns (optional) empty, a constant like Unknown goes into every text column that has a null, not only region. Columns whose type the value doesn't fit are skipped: Unknown leaves the numeric age alone, while 0 fills age and the text columns alike.
  • A space is not missing. A cell that contains only spaces is text, not null, so Fill Nulls leaves it alone. Fix such cells in the source file, where an empty cell is read as null.
  • Filling before training a model computes the median on all rows, including the rows you later test on. For modeling, use the train node's Advanced → Preprocessing options instead, see Feature Engineering.
  • To drop the rows instead, use Drop Nulls.

Exported Python

Click Export in the editor toolbar. The pandas and polars tabs show this code for the flow. It reads survey.csv from the script's folder and writes survey_filled.csv.

python
import pandas as pd

df_survey = pd.read_csv('survey.csv')
df_survey = df_survey.fillna({'age': df_survey['age'].median()})
df_survey = df_survey.copy()
for _col in ['region']:
    try:
        _fill = pd.array(['Unknown'], dtype=df_survey[_col].dtype)[0]
    except (ValueError, TypeError):
        continue  # the value does not fit this column's type
    df_survey[_col] = df_survey[_col].fillna(_fill)
df_survey.to_csv('survey_filled.csv', index=False)

See also