Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a Python code to fill all the missing values in a given dataframe
When working with datasets, missing values (NaN) are common. Pandas provides the interpolate() method to fill missing values using various interpolation techniques like linear, polynomial, or time-based methods.
Syntax
df.interpolate(method='linear', limit_direction='forward', limit=None)
Parameters
method − Interpolation technique ('linear', 'polynomial', 'spline', etc.)
limit_direction − Direction to fill ('forward', 'backward', 'both')
limit − Maximum number of consecutive NaNs to fill
Example
Let's create a DataFrame with missing values and apply linear interpolation ?
import pandas as pd
df = pd.DataFrame({"Id": [1, 2, 3, None, 5],
"Age": [12, 12, 14, 13, None],
"Mark": [80, 90, None, 95, 85]})
print("Original DataFrame:")
print(df)
print("\nAfter interpolation:")
print(df.interpolate(method='linear', limit_direction='forward', limit=2))
Original DataFrame:
Id Age Mark
0 1.0 12.0 80.0
1 2.0 12.0 90.0
2 3.0 14.0 NaN
3 NaN 13.0 95.0
4 5.0 NaN 85.0
After interpolation:
Id Age Mark
0 1.0 12.0 80.0
1 2.0 12.0 90.0
2 3.0 14.0 92.5
3 4.0 13.0 95.0
4 5.0 13.0 85.0
How It Works
Linear interpolation estimates missing values by drawing a straight line between known values:
Mark column − NaN at index 2 becomes 92.5 (midpoint between 90 and 95)
Id column − NaN at index 3 becomes 4.0 (between 3 and 5)
Age column − NaN at index 4 becomes 13.0 (forward fills from previous value)
Alternative Methods
You can also use fillna() for simpler filling strategies ?
import pandas as pd
df = pd.DataFrame({"Values": [1, None, 3, None, 5]})
# Forward fill
print("Forward fill:")
print(df.fillna(method='ffill'))
print("\nBackward fill:")
print(df.fillna(method='bfill'))
print("\nFill with mean:")
print(df.fillna(df.mean()))
Forward fill: Values 0 1.0 1 1.0 2 3.0 3 3.0 4 5.0 Backward fill: Values 0 1.0 1 3.0 2 3.0 3 5.0 4 5.0 Fill with mean: Values 0 1.0 1 3.0 2 3.0 3 3.0 4 5.0
Conclusion
Use interpolate() for intelligent missing value estimation based on surrounding data patterns. For simpler cases, fillna() with forward/backward fill or statistical measures works effectively.
