Pandasdrill

Cleaning Updated November 2026

fillna

df.fillna(value) replaces NaN values with the given value, and a dict fills each column differently: df.fillna({"age": 0, "city": "unknown"}). Use df.ffill() or df.bfill() to carry the previous or next valid value forward.

Syntax

df.fillna(value=None, inplace=False, limit=None)   |   df.ffill()   |   df.bfill()
PartWhat it is
valueScalar, dict (column to value) or Series/DataFrame aligned on labels
limitMaximum number of consecutive NaN values to fill
inplaceModify the frame in place instead of returning a copy

Examples

import pandas as pd
import numpy as np

df = pd.DataFrame({"name": ["Ana", "Ben", "Cara", "Dan"],
                   "age": [34, np.nan, 29, np.nan],
                   "city": ["Oslo", None, "Rome", "Rome"]})
df.fillna({"age": 0, "city": "unknown"})

Result: name age city 0 Ana 34.0 Oslo 1 Ben 0.0 unknown 2 Cara 29.0 Rome 3 Dan 0.0 Rome

df["age"] = df["age"].fillna(df["age"].mean())
df["age"].tolist()

Result: [34.0, 31.5, 29.0, 31.5]

Mean of the non-missing values (34 and 29) is 31.5.

df["city"].ffill()

Result: 0 Oslo 1 Oslo 2 Rome 3 Rome Name: city, dtype: object

Forward fill copies the last seen value down. Use bfill for the opposite direction.

Common mistakes

  • Nothing changes after fillna: it returns a new object; assign the result (df = df.fillna(0)) or pass inplace=True.
  • FutureWarning: fillna(method="ffill") is deprecated; call df.ffill() or df.bfill() directly.
  • Filling an integer column with 0 still shows floats (34.0): the column became float when NaN appeared; cast back with .astype(int) after filling.

Practice fillna

These drills check your answer on live data, free, no account needed.

  • Fill missing values in one columnSome players have no `points` recorded. Replace the missing points with 0, leave the missing `assists` as they are, and return the whole `scores` table.

Questions

How do I fill NaN with the mean of each column?

df.fillna(df.mean(numeric_only=True)) fills every numeric column with its own mean, since a Series value is aligned by column name.

How do I fill NaN only in some columns?

Pass a dict with just those columns, or assign per column: df["a"] = df["a"].fillna(0).

What is the difference between fillna and interpolate?

fillna uses a fixed value or the neighbor value; interpolate estimates missing numbers from surrounding values (linear by default), which suits time series.

Related

  • dropnaHow to drop NaN rows in pandas with dropna: any or all missing, only certain columns with subset, drop columns with axis=1, thresh for a minimum of values.
  • astypeHow to change a column type in pandas with astype and pd.to_numeric: text to numbers, float to int, strings, categories, nullable Int64, bad values and NaN.
  • transformHow groupby transform works in pandas: broadcast a group sum or mean to each row, share of group total, group-wise normalization, and transform versus agg.
  • pd.to_datetimeHow to parse dates in pandas with pd.to_datetime: formats, day-first dates, coercing bad values, and extracting year, month and weekday with the dt accessor.

Previous: mapNext: dropna