首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查找包含NaN的行的索引

查找包含NaN的行的索引
EN

Stack Overflow用户
提问于 2016-11-16 22:22:44
回答 1查看 189关注 0票数 2

在pandas数据帧中

代码语言:javascript
运行
复制
matrix

我想找出包含NaN的行(索引)。

对于在列中查找NaN,我会这样做

代码语言:javascript
运行
复制
  idx_nan = matrix.columns[np.isnan(matrix).any(axis=1)]

但它不能与matrix.rows一起工作

在行中查找项的等价物是什么?

EN

Stack Overflow用户

发布于 2016-11-16 22:25:10

我认为你需要有anyboolean indexingDataFrame.isnull

代码语言:javascript
运行
复制
print (df[df.isnull().any(1)].index)

示例:

代码语言:javascript
运行
复制
df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[np.nan,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)
   A  B    C  D  E  F
0  1  4  NaN  1  5  7
1  2  5  8.0  3  3  4
2  3  6  9.0  5  6  3

print (df[df.isnull().any(1)].index)
Int64Index([0], dtype='int64')

另一种解决方案:

代码语言:javascript
运行
复制
idx_nan = df[np.isnan(df).any(axis=1)].index
print (idx_nan)   
Int64Index([0], dtype='int64')

代码语言:javascript
运行
复制
idx_nan = df.index[np.isnan(df).any(axis=1)]
print (idx_nan) 
票数 5
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40634542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档