Python Pandas - Return Index without NaN values

To return Index without NaN values, use the index.dropna() method in Pandas. This method creates a new Index object with all NaN values removed, preserving the original data type.

Syntax

Index.dropna(how='any')

Parameters:

  • how ? {'any', 'all'}, default 'any'. Determines if row or column is removed from Index, when we have at least one NA or all NA.

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"\nNumber of elements: {index.size}")
print(f"Data type: {index.dtype}")
Original Pandas Index...
Index([50.0, 10.0, 70.0, nan, 90.0, 50.0, nan, nan, 30.0], dtype='float64')

Number of elements: 9
Data type: float64

Removing NaN Values

Use dropna() to create a new Index without NaN values ?

import pandas as pd
import numpy as np

index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])

# Drop NaN values
clean_index = index.dropna()

print("Index after removing NaN values...")
print(clean_index)
print(f"\nNumber of elements after dropna(): {clean_index.size}")
Index after removing NaN values...
Index([50.0, 10.0, 70.0, 90.0, 50.0, 30.0], dtype='float64')

Number of elements after dropna(): 6

Complete Example

Here's a comprehensive example showing the before and after comparison ?

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"\nNumber of elements in original index: {index.size}")
print(f"Data type: {index.dtype}")

# Drop only the NaN values
cleaned_index = index.dropna()
print("\nIndex after removing NaN values...")
print(cleaned_index)
print(f"\nNumber of elements after cleanup: {cleaned_index.size}")
Original Pandas Index...
Index([50.0, 10.0, 70.0, nan, 90.0, 50.0, nan, nan, 30.0], dtype='float64')

Number of elements in original index: 9
Data type: float64

Index after removing NaN values...
Index([50.0, 10.0, 70.0, 90.0, 50.0, 30.0], dtype='float64')

Number of elements after cleanup: 6

Key Points

  • The dropna() method returns a new Index object and doesn't modify the original
  • The data type is preserved after removing NaN values
  • All NaN values are removed, reducing the total number of elements
  • The method works with any Index containing NaN values

Conclusion

Use index.dropna() to efficiently remove NaN values from a Pandas Index. This method preserves the original data type while creating a clean Index object without any missing values.

Updated on: 2026-03-26T16:09:15+05:30

484 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements