[::-1] 顺序相反操作

[-1] 读取倒数第一个元素

[3::-1] 从下标为3(从0开始)的元素开始翻转读取


https://blog.csdn.net/HARDBIRD123/article/details/82261651

python pandas在最后一个非NaN值时停止填充na

df.notna()[::-1].idxmax()

df = df.where(df.bfill().isna(), df.ffill())
mask = df.notna()[::-1].idxmax().to_numpy() < df.index.to_numpy()[:, None]
df = df.where(mask, df.ffill())

你可以使用专门为此设计的 Series.last_valid_index (返回最后一个非NA/null值的索引),而只用 ffill 到该点。

df.apply(lambda x: x.loc[:x.last_valid_index()].ffill())

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.last_valid_index.html

https://www.qiniu.com/qfans/qnso-65886163