pandas 发现最后一个非NAN值 [英] Pandas find last non NAN value
本文介绍了 pandas 发现最后一个非NAN值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个pandas数据框,其中的列是日期,每一行是一个独立的时间序列.
I have a pandas data frame where the columns are dates and each row is an independent time series.
我尝试使用以下命令获取每行的最后一个值:
I try to get the last value of each row using the following:
df['last'] = df.iloc[:,-1]
但是,某些行的最后一列具有NAN值.
However some rows have NAN values in the last column.
如何获取行中最后一个非NAN值?
How can I get the last non NAN value in a row?
推荐答案
获取数据帧每一行中的最后一个非NaN
值:
Get last non NaN
value in each row of a dataframe:
df['last_value'] = df.ffill(axis=1).iloc[:, -1]
print (df)
这篇关于 pandas 发现最后一个非NAN值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文