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 program in Python to find the index for NaN value in a given series
When working with data analysis in Python, you often encounter NaN (Not a Number) values in your datasets. Finding the indices where these NaN values occur is a common task. This guide shows multiple approaches to locate NaN indices in a Pandas Series.
Creating a Sample Series
Let's first create a sample series containing NaN values ?
import pandas as pd import numpy as np l = [1, 2, 3, np.nan, 4, np.nan] data = pd.Series(l) print(data)
0 1.0 1 2.0 2 3.0 3 NaN 4 4.0 5 NaN dtype: float64
Method 1: Using Loop with np.isnan()
This method iterates through the series and checks each value using np.isnan() ?
import pandas as pd
import numpy as np
l = [1, 2, 3, np.nan, 4, np.nan]
data = pd.Series(l)
for i, j in data.items():
if np.isnan(j):
print("index is", i)
index is 3 index is 5
Method 2: Using isna() with Index
A more Pythonic approach using Pandas built−in methods ?
import pandas as pd
import numpy as np
l = [1, 2, 3, np.nan, 4, np.nan]
data = pd.Series(l)
nan_indices = data.index[data.isna()]
print("NaN indices:", list(nan_indices))
NaN indices: [3, 5]
Method 3: Using np.where()
Using NumPy's where() function to find NaN positions ?
import pandas as pd
import numpy as np
l = [1, 2, 3, np.nan, 4, np.nan]
data = pd.Series(l)
nan_indices = np.where(data.isna())[0]
print("NaN indices:", nan_indices)
NaN indices: [3 5]
Comparison
| Method | Performance | Best For |
|---|---|---|
Loop with np.isnan()
|
Slow | Learning/debugging |
data.index[data.isna()] |
Fast | Most Pandas operations |
np.where() |
Fastest | Large datasets |
Conclusion
Use data.index[data.isna()] for most cases as it's readable and efficient. For large datasets, np.where() offers the best performance. The loop method is useful for learning but avoid it in production code.
