用 Python 编写一个程序来查找给定系列中 NaN 值的索引
输入-
假设你有一个系列,
0 1.01 2.0
2 3.0
3 NaN
4 4.0
5 NaN
输出-
并且, NaN 索引的结果是,
index is 3index is 5
解决方案
为了解决这个问题,我们将按照下面给出的步骤 -
定义一个系列。
创建 for 循环并访问所有元素并将 if 条件设置为 check 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.01 2.0
2 3.0
3 NaN
4 4.0
5 NaN
dtype: float64
index is 3
index is 5
以上是 用 Python 编写一个程序来查找给定系列中 NaN 值的索引 的全部内容, 来源链接: utcz.com/z/349171.html