使用 Python 编写程序,查找给定序列中 NaN 值的索引。
输入 -
假设你有一个序列,
0 1.0 1 2.0 2 3.0 3 NaN 4 4.0 5 NaN
输出 -
并且,NaN 索引的结果是,
index is 3 index is 5
解决方案
为了解决这个问题,我们将遵循以下步骤 -
定义一个序列。
创建 for 循环并访问所有元素,设置 if 条件以检查 isnan()。最后打印索引位置。如下所示:
for i,j in data.items():
if(np.isnan(j)):
print("index is",i)示例
让我们看看下面的实现,以便更好地理解。
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)输出
0 1.0 1 2.0 2 3.0 3 NaN 4 4.0 5 NaN dtype: float64 index is 3 index is 5
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP