最后识别连续的非 nan 值

Identify consecutive not nan values at the end

如何获取table中连续非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)]

试试这个:

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

输出:

    col
6   4.0
7   5.0
8   6.0

这对我有用:

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

这是您可以执行此操作的另一种方法 -

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