- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in Python to find the index for NaN value in a given series
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
Advertisements
To Continue Learning Please Login
Login with Google