我有一个这样的数据框,
ID Cus_ID cl_id
0 5.0 200 0
1 NaN 200 0
2 NaN 200 1
3 14.0 200 2
4 15.0 200 2
5 16.0 200 2
6 NaN 200 3
从上面的数据框中,我想提取第 0 行到第 4 行并检查“ID”列中是否有任何值具有
NaN
值。我试过这个,rows_needed = [0,1,2,3,4]
df.iloc[rows_needed,0].isnull().index.tolist()
但我得到以下信息,
[0, 1, 2, 3, 4]
我期待得到
[1,2]
的索引.我怎样才能得到我想要的输出?当我这样做时,
df.iloc[rows_needed,0].isnull()
我明白了,
0 False
1 True
2 True
3 False
4 False
Name: ID, dtype: bool
不知道我在哪里犯了没有得到我的输出的错误。
最佳答案
你非常非常接近,你需要做的是链式.iloc
和 .loc
与 ==TRUE
得到你的结果
your_indices = (df.iloc[rows_needed]
.loc[df.ID.isnull()==True]
.index.tolist())
print(your_indices)
[1, 2]
关于python-3.x - 从一组选定的行中获取 NaN 值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927017/