在Pandas DataFrame中找到第一个和最后一个非NaN值 [英] Locate first and last non NaN values in a Pandas DataFrame

查看:835
本文介绍了在Pandas DataFrame中找到第一个和最后一个非NaN值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Pandas DataFrame 按日期索引。有许多列,但是很多列只是部分时间序列的填充。我想找到第一个和最后一个值 - NaN 值的位置,以便我可以提取日期,并查看特定列的时间序列的时间长度

I have a Pandas DataFrame indexed by date. There a number of columns but many columns are only populated for part of the time series. I'd like to find where the first and last values non-NaN values are located so that I can extracts the dates and see how long the time series is for a particular column.

有人可以指出我正确的方向,我可以如何去做这样的事情吗?感谢提前。

Could somebody point me in the right direction as to how I could go about doing something like this? Thanks in advance.

推荐答案

@ behzad.nouri的解决方案完美地返回第一个和最后一个非 NaN值使用 Series.first_valid_index Series.last_valid_index

@behzad.nouri's solution worked perfectly to return the first and last non-NaN values using Series.first_valid_index and Series.last_valid_index, respectively.

这篇关于在Pandas DataFrame中找到第一个和最后一个非NaN值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆