Methods for Handling Missing Data
Now suppose our marks Series contains missing values.
marks = pd.Series([85, 72, 90, None, 88])
Pandas provides several methods to handle these missing values.
dropna(): The dropna() method removes missing values from the Series.
For example:
marks.dropna()
output:
0 85.0
1 72.0
2 90.0
4 88.0
dtype: float64
The missing value is removed, while the remaining values are returned.
fillna(): The fillna() method is used to replace missing values with a specified value.
For example:
marks.fillna(0)
output:
0 85.0
1 72.0
2 90.0
3 0.0
4 88.0
dtype: float64
Here, the missing value is replaced with 0.
We can also replace the missing value with something more meaningful.
For example:
marks.fillna(marks.mean())
Here, the missing mark is replaced with the average of the available marks.
interpolate(): The interpolate() method fills missing values by estimating a value based on surrounding data.
For example:
marks = pd.Series([80, 85, None, 95])
marks.interpolate()
output:
0 80.0
1 85.0
2 90.0
3 95.0
dtype: float64
Here, Pandas estimates the missing value between 85 and 95 as 90.
This method is especially useful when the data follows a sequence or trend.