我有以下格式的数据集。它有48列和大约200000行。你知道吗
slot1,slot2,slot3,slot4,slot5,slot6...,slot45,slot46,slot47,slot48
1,2,3,4,5,6,7,......,45,46,47,48
3.5,5.2,2,5.6,...............
我想把这个数据集改成如下的样子,其中N小于48(可能是24或12等…)列标题无关紧要。 当N=4时
slotNew1,slotNew2,slotNew3,slotNew4
1,2,3,4
5,6,7,8
......
45,46,47,48
3.5,5.2,2,5.6
............
我可以逐行读取,然后拆分每一行并附加到新的数据帧。但这是非常低效的。有没有更有效更快的方法?你知道吗
你可以试试这个
代码将数据提取到
numpy.ndarray
,对其进行整形,并创建所需维度的新数据集。你知道吗示例:
另一种方法
制作块后使用
pandas.explode
。给定df
:用
chunks
除:输出:
与
numpy.reshape
相比,这种方法的优势在于它可以处理N
不是一个因子的情况:输出:
相关问题 更多 >
编程相关推荐