To return Index without NaN values, use the index.dropna() method in Pandas. At first, import the required libraries −
import pandas as pd import numpy as np
Creating Pandas index with some NaN values as well −
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
Display the Pandas index −
print("Pandas Index...\n",index)Drop only the NaN values −
print("\nThe Index object after removing NaN values...\n",index.dropna())Example
Following is the code −
import pandas as pd
import numpy as np
# Creating Pandas index with some NaN values as well
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
# Display the Pandas index
print("Pandas Index...\n",index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)
# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)
# Drop only the NaN values
print("\nThe Index object after removing NaN values...\n",index.dropna())Output
This will produce the following output −
Pandas Index... Float64Index([50.0, 10.0, 70.0, nan, 90.0, 50.0, nan, nan, 30.0], dtype='float64') Number of elements in the index... 9 The dtype object... float64 The Index object after removing NaN values... Float64Index([50.0, 10.0, 70.0, 90.0, 50.0, 30.0], dtype='float64')