[英]How to get new pandas dataframe with max value of k consecutive rows?
I have this pandas dataframe:我有这个 pandas dataframe:
ts = pd.Series([2372, 4356, 3034, 1502, 676, 4187, 2634, 1002])
What I would like to get is a dataframe which exists of the max value of four consecutive rows of the column and keeps index of row 0, 4, 8 etc.我想得到的是一个 dataframe ,它存在列的四个连续行的最大值,并保持第 0、4、8 行等的索引。
In this case, this means a new dataframe should look like this one:在这种情况下,这意味着新的 dataframe 应该如下所示:
0 4356
4 4187
How could I do this?我怎么能这样做?
Use numpy.arange
with Groupby.agg
:将
numpy.arange
与Groupby.agg
一起使用:
In [899]: df = ts.groupby(np.arange(len(ts))//4).agg(['idxmax','max'])
In [900]: df
Out[900]:
idxmax max
0 1 4356
1 5 4187
Try:尝试:
ts.groupby((np.arange(len(ts))//4)*4).max()
Output: Output:
0 4356
4 4187
dtype: int64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.