0
0
PandasHow-ToBeginner · 3 min read

How to Use fillna in pandas to Handle Missing Data

In pandas, use fillna() to replace missing values (NaN) in a DataFrame or Series with a specified value or method. This helps clean data by filling gaps with constants, forward-fill, or backward-fill. The method returns a new object or modifies in place if specified.
📐

Syntax

The fillna() method has several parameters:

  • value: The value to replace missing data with (number, string, dict, or Series).
  • method: Use 'ffill' (forward fill) or 'bfill' (backward fill) to propagate non-missing values.
  • axis: Choose rows (0) or columns (1) to fill along.
  • inplace: If True, modifies the original object instead of returning a new one.
  • limit: Maximum number of consecutive NaNs to fill.
python
DataFrame.fillna(value=None, method=None, axis=0, inplace=False, limit=None)
💻

Example

This example shows how to fill missing values in a DataFrame with a constant and with forward fill method.

python
import pandas as pd
import numpy as np

data = {'A': [1, np.nan, 3, np.nan], 'B': [np.nan, 2, np.nan, 4]}
df = pd.DataFrame(data)

# Fill NaN with a constant value
filled_constant = df.fillna(0)

# Fill NaN using forward fill method
filled_ffill = df.fillna(method='ffill')

print('Original DataFrame:')
print(df)
print('\nFill NaN with 0:')
print(filled_constant)
print('\nFill NaN with forward fill:')
print(filled_ffill)
Output
Original DataFrame: A B 0 1.0 NaN 1 NaN 2.0 2 3.0 NaN 3 NaN 4.0 Fill NaN with 0: A B 0 1.0 0.0 1 0.0 2.0 2 3.0 0.0 3 0.0 4.0 Fill NaN with forward fill: A B 0 1.0 NaN 1 1.0 2.0 2 3.0 2.0 3 3.0 4.0
⚠️

Common Pitfalls

Common mistakes when using fillna() include:

  • Using method and value together, which is not allowed.
  • Forgetting to set inplace=True if you want to modify the original DataFrame.
  • Not specifying limit when you want to restrict how many NaNs get filled.
  • Trying to fill with incompatible types (e.g., filling numeric columns with strings).
python
import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [1, np.nan, 3]})

# Wrong: Using value and method together raises error
try:
    df.fillna(value=0, method='ffill')
except ValueError as e:
    print(f'Error: {e}')

# Right: Use either value or method
filled = df.fillna(method='ffill')
print(filled)
Output
Error: Cannot specify both 'value' and 'method' A 0 1.0 1 1.0 2 3.0
📊

Quick Reference

Here is a quick summary of fillna() options:

ParameterDescriptionExample
valueValue to replace NaNsdf.fillna(0)
methodFill method: 'ffill' or 'bfill'df.fillna(method='ffill')
axisAxis to fill along (0=rows, 1=columns)df.fillna(0, axis=1)
inplaceModify original DataFrame if Truedf.fillna(0, inplace=True)
limitMax number of NaNs to filldf.fillna(0, limit=1)

Key Takeaways

Use fillna() to replace missing values in pandas DataFrames or Series easily.
Choose between filling with a constant value or using methods like forward fill.
Do not use both value and method parameters together in fillna().
Set inplace=True to modify the original data directly.
Use limit to control how many consecutive NaNs get filled.