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
Python Pandas - Fill NaN values with the specified value in an Index object
To fill NaN values with the specified value in an Index object, use the index.fillna() method in Pandas. This method replaces all NaN values with a specified replacement value and returns a new Index object.
Syntax
Index.fillna(value=None, downcast=None)
Parameters
value: The value to replace NaN values with. Can be a scalar value, string, or any valid data type.
downcast: Optional parameter to downcast the data type if possible.
Creating Index with NaN Values
First, let's create a Pandas Index containing some NaN values ?
import pandas as pd
import numpy as np
# Creating Pandas index with some NaN values
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
print("Original Pandas Index:")
print(index)
print(f"\nData type: {index.dtype}")
print(f"Number of elements: {index.size}")
Original Pandas Index: Float64Index([50.0, 10.0, 70.0, nan, 90.0, 50.0, nan, nan, 30.0], dtype='float64') Data type: float64 Number of elements: 9
Filling NaN with Numeric Values
Replace NaN values with a numeric value ?
import pandas as pd
import numpy as np
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
# Fill NaN with numeric value
filled_numeric = index.fillna(0)
print("After filling NaN with 0:")
print(filled_numeric)
print(f"Data type: {filled_numeric.dtype}")
After filling NaN with 0: Float64Index([50.0, 10.0, 70.0, 0.0, 90.0, 50.0, 0.0, 0.0, 30.0], dtype='float64') Data type: float64
Filling NaN with String Values
Replace NaN values with a string value. Note that this changes the data type to object ?
import pandas as pd
import numpy as np
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
# Fill NaN with string value
filled_string = index.fillna('Missing')
print("After filling NaN with 'Missing':")
print(filled_string)
print(f"Data type: {filled_string.dtype}")
After filling NaN with 'Missing': Index([50.0, 10.0, 70.0, 'Missing', 90.0, 50.0, 'Missing', 'Missing', 30.0], dtype='object') Data type: object
Complete Example
Here's a comprehensive example demonstrating different fill values ?
import pandas as pd
import numpy as np
# Create Index with NaN values
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
print("Original Index:")
print(index)
# Fill with different values
print("\nFilling NaN with -1:")
print(index.fillna(-1))
print("\nFilling NaN with 'Unknown':")
print(index.fillna('Unknown'))
print("\nFilling NaN with mean of non-NaN values:")
mean_value = index.dropna().mean()
print(index.fillna(mean_value))
Original Index: Float64Index([50.0, 10.0, 70.0, nan, 90.0, 50.0, nan, nan, 30.0], dtype='float64') Filling NaN with -1: Float64Index([50.0, 10.0, 70.0, -1.0, 90.0, 50.0, -1.0, -1.0, 30.0], dtype='float64') Filling NaN with 'Unknown': Index([50.0, 10.0, 70.0, 'Unknown', 90.0, 50.0, 'Unknown', 'Unknown', 30.0], dtype='object') Filling NaN with mean of non-NaN values: Float64Index([50.0, 10.0, 70.0, 50.0, 90.0, 50.0, 50.0, 50.0, 30.0], dtype='float64')
Key Points
? The fillna() method returns a new Index object and doesn't modify the original
? When filling with string values, the data type changes from numeric to object
? You can use any scalar value as replacement: numbers, strings, booleans, etc.
? The method preserves the original Index structure while only replacing NaN values
Conclusion
The fillna() method is essential for handling missing data in Pandas Index objects. Use numeric values to maintain data type consistency, or strings when categorical representation is needed.
