【问题标题】:Identify consecutive not nan values at the end在末尾识别连续的非 nan 值
【发布时间】:2021-06-05 05:21:27
【问题描述】:

如何获取表中连续非NaN 值的最后一部分?例如:

   col
0  1.0
1  2.0
2  NaN
3  NaN
4  3.0
5  NaN
6  4.0
7  5.0
8  6.0

想要的结果:

   col
6  4.0
7  5.0
8  6.0

我的解决方案:

df[df.col[::-1].isna().cumsum()[::-1].eq(0)]

【问题讨论】:

    标签: python pandas numpy nan missing-data


    【解决方案1】:

    试试这个:

    last_index = df[df.col.isna()].index[-1]
    df.iloc[last_index + 1:]
    

    输出:

        col
    6   4.0
    7   5.0
    8   6.0
    

    【讨论】:

      【解决方案2】:

      这是另一种方法-

      df[df.loc[::-1, 'col'].isna().cumsum()[::-1]==0]
      
          col
      6   4.0
      7   5.0
      8   6.0
      

      【讨论】:

        【解决方案3】:

        这对我有用:

        last_index = np.where(df.col.isna())[0][-1]
        df.iloc[last_index+1:]
        

        【讨论】:

          猜你喜欢
          • 2015-05-14
          • 2020-05-11
          • 1970-01-01
          • 2021-04-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-26
          相关资源
          最近更新 更多