我想将同一数据集的五行合并合并为一个数据集,我有700行,我想每隔五行进行合并合并:
A B C D E F G
1 10,11,12,13,14,15,16
2 17,18,19,20,21,22,23
3 24,25,26,27,28,29,30
4 31,32,33,34,35,36,37
5 38,39,40,41,42,43,44
.
.
.
.
.
700
字符串
合并前五行后..我的第一行应该是这样的:
A B C D E F G A B C D E F G A B C D E F G A B C D E F G A B C D E F G
1 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44
型
6条答案
按热度按时间ruoxqz4g1#
如果你能保证你拥有的总行数是5的倍数,那么使用
numpy
将是解决这个问题的最有效的方法:字符串
o3imoua42#
您可以:
字符串
svmlkihl3#
看看这是否有助于回答你的问题unstack把列变成行,一旦我们有了列中的数据,我们只需要把它转置。reset_index把结果序列变成一个数组。原始的列名变成一个索引,所以当我们转置时,我们有了你在列中声明的列。
个字符
投票和/或接受,如果答案有帮助
6ljaweal4#
最简单的方法是将你的数组转换成一个numpy数组,重新塑造它,然后将它转换回一个新的数组。
编辑:
字符串
piok6c0g5#
pandas中数据的堆叠和拆堆
表格中的数据通常以多种方式呈现。长格式(“整洁数据”)指的是堆叠在几列中的数据。其中一列将具有关于值的分类指示符。相比之下,宽格式(“堆叠数据”)是每个类别都有自己的列。
在您的示例中,您呈现了宽格式的数据,并试图将其转换为长格式。pandas.melt、pandas.groupby、pandas.pivot、pandas.stack、pandas.unstack和pandas.reset_index是帮助在这些格式之间进行转换的函数。
从您的原始框架开始:
字符串
**使用pandas.melt将其转换为长格式,然后排序以获得您请求的数据:**忽略索引选项有助于我们稍后将其恢复为宽格式。
型
**使用groupby、unstack和reset_index将其转换回宽格式。**这通常是一个困难得多的过程,它依赖于按值堆叠列、其他列、索引和堆叠变量分组,然后取消堆叠并重置索引。
型
这些东西可能相当困难,需要反复试验。我建议你把问题做得更小,然后把它们弄乱。这样你就可以弄清楚函数是如何工作的。
kqhtkvqz6#
尝试使用
arange()
和floordiv
来获得每5个的组,然后用组创建一个新的df
。即使你的df
不能被5整除,这也应该有效。字符串
或
型
输出量:
型