# Imputation

> Fill the missing cells of a table from its other columns.

`impute` fills empty cells with their most likely values, learned from the other columns. This fills the 177 missing ages in
the Titanic passenger list:

```python
import pandas as pd
from yhatlabs import Client

titanic = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")
titanic = titanic.drop(columns=["PassengerId", "Name", "Ticket", "Cabin"])
print("missing ages before:", titanic["Age"].isna().sum())

yh = Client()
filled = yh.impute(titanic, columns=["Age"])
print("missing ages after:", filled["Age"].isna().sum())
print(filled[filled.attrs["filled"]["Age"]][["Pclass", "Sex", "SibSp", "Age"]].head(3).round(1).to_string(index=False))
```

Output:

```
missing ages before: 177
missing ages after: 0
 Pclass    Sex  SibSp  Age
  3   male      0 29.4
  2   male      0 33.4
  3 female      0 20.5
```

- For each column with gaps, the model learns that column from the other columns on the rows where it is present, and
  predicts it for the rest: one prediction call per column.
- Numeric columns with many distinct values are treated as regression; the rest as classification.
- `columns=[...]` limits it to some columns. `filled.attrs["filled"]` marks every filled cell.
- A column with fewer than 10 observed values, or a single distinct value, is left as it is, with a warning.

For gaps inside a time series, use [`fill_gaps`](https://yhatlabs.com/models/docs/time-series/anomalies-and-gaps#fill-gaps) instead: it fills from the
series' own history.
