pandas -在没有Nan值的情况下寻找最长的伸展时间 [英] Pandas - Find longest stretch without Nan values

查看:45
本文介绍了 pandas -在没有Nan值的情况下寻找最长的伸展时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框"df",其示例如下:

I have a pandas dataframe "df", a sample of which is below:

   time  x
0  1     1
1  2     Nan 
2  3     3
3  4     Nan
4  5     8
5  6     7
6  7     5
7  8     Nan

实际框架要大得多.我试图找到"x"系列中最长的非NaN值,并打印出该帧的开始和结束索引.这可能吗?

The real frame is much bigger. I am trying to find the longest stretch of non NaN values in the "x" series, and print out the starting and ending index for this frame. Is this possible?

谢谢

推荐答案

这是NumPy工具的矢量化方法-

Here's a vectorized approach with NumPy tools -

a = df.x.values  # Extract out relevant column from dataframe as array
m = np.concatenate(( [True], np.isnan(a), [True] ))  # Mask
ss = np.flatnonzero(m[1:] != m[:-1]).reshape(-1,2)   # Start-stop limits
start,stop = ss[(ss[:,1] - ss[:,0]).argmax()]  # Get max interval, interval limits

样品运行-

In [474]: a
Out[474]: 
array([  1.,  nan,   3.,  nan,  nan,  nan,  nan,   8.,   7.,   5.,   2.,
         5.,  nan,  nan])

In [475]: start, stop
Out[475]: (7, 12)

设置间隔,使每个开始和停止之间的差值可以使我们知道每个间隔的长度.因此,如果要获取非零元素的最后一个索引,则使用ending index,我们需要从stop中减去一个.

The intervals are set such that the difference between each start and stop would give us the length of each interval. So, by ending index if you meant to get the last index of non-zero element, we need to subtract one from stop.

这篇关于 pandas -在没有Nan值的情况下寻找最长的伸展时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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