用 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条件设置为checkisnan()。最后打印索引位置。它定义如下,
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
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短