2 回答

TA贡献1850条经验 获得超11个赞
stack与通过模和整数除法创建的多索引一起使用:
a = np.arange(len(df.columns))
df.columns = [a % 3, a // 3]
df = df.stack().sort_index(level=1).reset_index(drop=True)
print (df)
0 1 2
0 09.08.00 31.6875 -0.017442
1 10.08.00 31.7031 0.000492
2 11.08.00 31.7656 0.001971
3 14.08.00 31.5625 -0.006394
4 15.08.00 31.5000 -0.001980
5 17.10.00 59.1250 0.002119
6 18.10.00 59.1250 0.000000
7 19.10.00 59.3125 0.003171
8 20.10.00 59.5625 0.004215
9 23.10.00 59.1250 -0.007345
Numpy 解决方案是可能的,但因为最后有一些字符串需要转换为floats:
a = np.reshape(df.values,(len(df), -1, 3)).swapaxes(0,1).reshape(-1, 3)
df = pd.DataFrame(a)
df[[1,2]] = df[[1,2]].astype(float)
print (df)
0 1 2
0 09.08.00 31.6875 -0.017442
1 10.08.00 31.7031 0.000492
2 11.08.00 31.7656 0.001971
3 14.08.00 31.5625 -0.006394
4 15.08.00 31.5000 -0.001980
5 17.10.00 59.1250 0.002119
6 18.10.00 59.1250 0.000000
7 19.10.00 59.3125 0.003171
8 20.10.00 59.5625 0.004215
9 23.10.00 59.1250 -0.007345
添加回答
举报