按连续索引号分组 [英] Group by consecutive index numbers

查看:88
本文介绍了按连续索引号分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以对连续的索引号进行分组并将组移动到不同的列中.这是我正在使用的DataFrame的示例:

I was wondering if there is a way to groupby consecutive index numbers and move the groups in different columns. Here is an example of the DataFrame I'm using:

                 0
0     19218.965703
1     19247.621650
2     19232.651322
9     19279.216956
10    19330.087371
11    19304.316973

我的想法是通过顺序索引号进行汇总,并得到如下所示的信息:

And my idea is to gruoup by sequential index numbers and get something like this:

                 0             1
0     19218.965703  19279.216956    
1     19247.621650  19330.087371
2     19232.651322  19304.316973

我一直试图将数据分成3个块,然后再进行groupby,但是我一直在寻找更多可用于对顺序索引号进行分组和重新排列的东西. 谢谢!

Ive been trying to split my data by blocks of 3 and then groupby but I was looking more about something that can be used to group and rearrange sequential index numbers. Thank you!

推荐答案

这是一种方法:

from more_itertools import consecutive_groups
final=pd.concat([df.loc[i].reset_index(drop=True) 
                    for i in consecutive_groups(df.index)],axis=1)
final.columns=range(len(final.columns))
print(final)


              0             1
0  19218.965703  19279.216956
1  19247.621650  19330.087371
2  19232.651322  19304.316973

这篇关于按连续索引号分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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