Write a program in Python to find the index for NaN value in a given series

PythonPandasServer Side ProgrammingProgramming

Input

Assume, you have a series,

0    1.0
1    2.0
2    3.0
3    NaN
4    4.0
5    NaN

Output

 And, the result for NaN index is,

index is 3
index is 5

Solution

To solve this, we will follow the steps given below −

  • Define a Series.

  • Create for loop and access all the elements and set if condition to check isnan(). Finally print the index position. It is defined below,

for i,j in data.items():
if(np.isnan(j)):
   print("index is",i)

Example

Let us see the following implementation to get a better understanding.

import pandas as pd
import numpy as np
l = [1,2,3,np.nan,4,np.nan]
data = pd.Series(l)
print(data)
for i,j in data.items():
   if(np.isnan(j)):
      print("index is",i)

Output

0    1.0
1    2.0
2    3.0
3    NaN
4    4.0
5    NaN
dtype: float64
index is 3
index is 5
raja
Published on 10-Feb-2021 13:17:38
Advertisements