Pandas Missing Value Handling
Real-world data often contains missing values (NaN), and Pandas provides a rich set of functions for handling missing data. This section explains in detail how to use methods such as fillna, dropna, and interpolate.
Representation of missing values
In Pandas, use NaN(Not a Number) represents missing values and comes from the NumPy library.
Example
import
pandas
as
pd
import
numpy
as
np
Create data containing missing values
s
=
pd.
Series
(
[
1
,
2
,
np.
nan
,
4
,
5
]
)
(
"Series containing NaN:"
)
(
s
)
(
f
"Has missing values: {s.isna().any()}"
)
(
)
Missing values in a DataFrame
df
=
pd.
DataFrame
(
{
"A"
:
[
1
,
2
,
np.
nan
,
4
]
,
"B"
:
[
np.
nan
,
2
,
3
,
4
]
,
"C"
:
[
1
,
2
,
3
,
np.
nan
]
}
)
(
"DataFrame with missing values:"
)
(
df
)
(
)
Detect missing values
(
"Missing value locations:"
)
(
df.
isna
(
)
)
(
)
Count missing values in each column
(
"Missing value count per column:"
)
(
df.
isna
(
)
.
sum
(
)
)
In Pandas,
NaN、None、pandas.NAare all recognized as missing values. Useisna()orisnull()to detect all of these missing values consistently.
dropna: remove missing values
Drop rows
Example
import
pandas
as
pd
import
numpy
as
np
df
=
pd.
DataFrame
(
{
"A"
:
[
1
,
2
,
np.
nan
,
4
]
,
"B"
:
[
1
,
np.
nan
,
3
,
4
]
,
"C"
:
[
1
,
2
,
3
,
np.
nan
]
}
)
(
"Original data:"
)
(
df
)
(
)
Drop rows containing missing values (default)
(
"Rows with missing values dropped:"
)
(
df.
dropna
(
)
)
(
)
how='all': drop only when all values are missing
(
"Only rows with all values missing are dropped:"
)
(
df.
dropna
(
how
=
"all"
)
)
(
)
thresh: keep only rows with at least N non-missing values
(
"Keep only rows with at least 2 non-missing values:"
)
(
df.
dropna
(
thresh
=
2
)
)
Drop columns
Example
import
pandas
as
pd
import
numpy
as
np
df
=
pd.
DataFrame
(
{
"A"
:
[
1
,
2
,
np.
nan
,
4
]
,
"B"
:
[
np.
nan
,
np.
nan
,
np.
nan
,
np.
nan
]
,
All missing
"C"
:
[
1
,
2
,
3
,
4
]
}
)
(
"Original data:"
)
(
df
)
(
)
Drop columns containing missing values
(
"Columns with missing values dropped:"
)
(
df.
dropna
(
axis
=
1
)
)
(
)
how='all': drop columns where all values are missing
(
"Columns with all values missing dropped:"
)
(
df.
dropna
(
axis
=
1
,
how
=
"all"
)
)
fillna: fill missing values
Fill with a fixed value
Example
import
pandas
as
pd
import
numpy
as
np
df
=
pd.
DataFrame
(
{
"A"
:
[
1
,
2
,
np.
nan
,
4
,
5
]
,
"B"
:
[
np.
nan
,
2
,
3
,
np.
nan
,
5
]
,
"C"
:
[
1
,
2
,
3
,
4
,
np.
nan
]
}
)
(
"Original data:"
)
(
df
)
(
)
Fill with 0
(
"Filled with 0:"
)
(
df.
fillna
(
0
)
)
(
)
Fill different columns with specified values
(
"Different columns filled with different values:"
)
(
df.
fillna
(
{
"A"
:
0
,
"B"
:
99
,
"C"
: -
1
}
)
)
(
)
Fill with the previous value (forward fill)
(
"Forward fill:"
)
(
df.
fillna
(
method
=
"ffill"
)
)
(
)
Fill with the next value (backward fill)
(
"Backward fill:"
)
(
df.
fillna
(
method
=
"bfill"
)
)
Fill with statistical values
Example
import
pandas
as
pd
import
numpy
as
np
df
=
pd.
DataFrame
(
{
"A"
:
[
1
,
2
,
np.
nan
,
4
,
5
,
6
]
,
"B"
:
[
10
,
np.
nan
,
30
,
np.
nan
,
50
,
60
]
}
)
(
"Original data:"
)
(
df
)
(
)
Fill with the mean
(
"Filled with the mean:"
)
(
df.
fillna
(
df.
mean
(
)
)
)
(
)
Fill with the median
(
"Filled with the median:"
)
(
df.
fillna
(
df.
median
(
)
)
)
(
)
Fill each column separately
(
"Column A filled with the mean, column B with 0:"
)
(
df.
fillna
(
{
"A"
: df
[
"A"
]
.
mean
(
)
,
"B"
:
0
}
)
)
Forward and backward fills are especially useful for time-series data because they preserve continuity. Which one to choose depends on the business meaning of the data.
interpolate: fill by interpolation
Interpolation is a smarter way to fill, estimating missing values from neighboring data.
Linear interpolation
Example
import
pandas
as
pd
import
numpy
as
np
s
=
pd.
Series
(
[
1
,
2
,
np.
nan
,
4
,
5
,
np.
nan
,
7
]
)
(
"Original Series:"
)
(
s
)
(
)
Linear interpolation (default)
(
"Linear interpolation:"
)
(
s.
interpolate
(
)
)
(
)
Specify an interpolation method
(
"Time-weighted interpolation (time series):"
)
s2
=
pd.
Series
(
[
1
,
np.
nan
,
np.
nan
,
4
]
,
index
=
pd.
date_range
(
"2024-01-01"
,
periods
=
4
,
freq
=
"D"
)
)
(
s2.
interpolate
(
method
=
"time"
)
)
(
)
Fill with nearby values
(
"Filled with nearby values:"
)
(
s.
interpolate
(
method
=
"nearest"
)
)
DataFrame interpolation
Example
import
pandas
as
pd
import
numpy
as
np
df
=
pd.
DataFrame
(
{
"A"
:
[
1
,
2
,
np.
nan
,
4
,
5
]
,
"B"
:
[
10
,
np.
nan
,
30
,
40
,
np.
nan
]
}
)
(
"Original data:"
)
(
df
)
(
)
Interpolate on the original DataFrame
df_interpolated
=
df.
interpolate
(
method
=
"linear"
)
(
"After linear interpolation:"
)
(
df_interpolated
)
(
)
Limit the interpolation range
(
"Fill only the first and last of consecutive missing values (at most 1):"
)
(
df.
interpolate
(
limit
=
1
)
)
Practical example: handling real data
Example
import
pandas
as
pd
import
numpy
as
np
Simulate realistic business data
np.
random
.
seed
(
42
)
n
=
20
df
=
pd.
DataFrame
(
{
"date"
: pd.
date_range
(
"2024-01-01"
,
periods
=
n
)
,
"Sales"
: np.
random
.
choice
(
[
100
,
200
,
np.
nan
,
300
,
np.
nan
]
,
n
)
,
"customer_count"
: np.
random
.
choice
(
[
10
,
np.
nan
,
20
,
30
]
,
n
)
,
"Conversion Rate"
: np.
random
.
choice
(
[
0.05
,
0.1
,
np.
nan
,
0.15
]
,
n
)
}
)
(
"Missing values in the original data:"
)
(
df.
isna
(
)
.
sum
(
)
)
(
)
Handling strategy:
1. Fill sales with the average of neighboring values (business allows fluctuation)
df
[
"Sales"
]
=
df
[
"Sales"
]
.
interpolate
(
)
2. Fill customer count with the mean
df
[
"customer_count"
]
=
df
[
"customer_count"
]
.
fillna
(
df
[
"customer_count"
]
.
mean
(
)
)
3. Fill conversion rate with 0 (meaning no conversion)
df
[
"Conversion Rate"
]
=
df
[
"Conversion Rate"
]
.
fillna
(
0
)
(
"Processed data:"
)
(
df
)
(
)
(
"Missing values after processing:"
)
(
df.
isna
(
)
.
sum
(
)
)
Common questions
1. Does fillna create a new object or modify in place?
fillna By default it returns a new object; use inplace=True to modify in place.
2. Missing values remain after interpolation
If missing values are at the beginning or end of the series, interpolation cannot fill them and extra handling is needed.
3. Distinguish NaN from empty strings
Empty string "" is not a missing value; you need to use replace("", np.nan) to convert.
Consider the business meaning when choosing a fill method: forward fill suits time-series data, backward fill suits static data, the mean suits numeric data, and interpolation suits continuous data.