【问题标题】:Get all Row and Columns Positions of NaN values in pandas [duplicate]获取熊猫中NaN值的所有行和列位置[重复]
【发布时间】:2017-06-28 05:16:51
【问题描述】:

如何获取 Pandas 中有 NAN 值的所有位置。例如,

sample = pd.DataFrame(np.zeros(shape=[5,5]))
sample.iloc[0,0] = np.nan
sample.iloc[2,3] = np.nan
sample.iloc[4,3] = np.nan

是一个 Pandas 数据框,其中 (0,0)、(2,3) 和 (4,3) 等位置具有 nan 值。因此,我想要一个这样的元组列表

[(0,0),(2,3),(4,3)]

我能得到这个吗?

问候

【问题讨论】:

标签: python pandas tuples nan


【解决方案1】:

在这种情况下,由于您的列和索引值是行号和列号,您可以首先使用 isnull 将每个条目转换为布尔值,然后使用 lambda 函数过滤括号中的真实值,然后将选定的索引到列表中。

sample.isnull().stack()[lambda x: x].index.tolist()
[(0, 0), (2, 3), (4, 3)]

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 2018-03-16
    • 2018-05-05
    • 2020-11-28
    • 1970-01-01
    • 2021-07-01
    • 2019-11-08
    • 2020-12-10
    • 2021-11-04
    相关资源
    最近更新 更多