代码之家  ›  专栏  ›  技术社区  ›  Vikas NS

如何存储通过pd.dropna删除的行的行数(df.dropna(axis=0))

  •  0
  • Vikas NS  · 技术社区  · 6 年前

    下面给出了示例数据框

         c1    c2
    0    1      2
    1    nan    4
    2    1      nan
    3    4       1
    4    nan    nan
    

    使用df.dropna(axis=0)函数删除后,我希望有一个行索引列表,如下所示。

     [1,2,4]
    
    3 回复  |  直到 6 年前
        1
  •  3
  •   Scott Boston    6 年前

    df.index[df.isnull().any(1)].tolist()
    

    [1, 2, 4]
    

    df = df.dropna(axis=0)
    

    1. any
        2
  •  5
  •   BENY    6 年前

    difference

    df.index.difference(df.dropna().index).tolist()
    Out[420]: [1, 2, 4]
    

    df.index[df.isnull().any(1)].tolist()
    Out[424]: [1, 2, 4]
    
        3
  •  2
  •   W Stokvis    6 年前

    np.where NaN

    np.where(df.isna())
    (array([1, 2, 4, 4]), array([0, 1, 0, 1]))
    

    list(set(np.where(df.isna())[0]))
    [1, 2, 4]