如下所述,我在一行中有数据,但我想将行转换为8列,并将列索引转换为var1,var2,var3,var4,var5,var6,var7,var8

0   0.008985
1   0.486024
2   0.800637
3   0.006264
4   5.030551
5   0.001959
6   0.002210
7   0.005949
8   0.008985
9   0.689814
10  0.889638
11  0.007462
12  5.589760
13  0.005739
14  0.006474
15  0.004770
16  0.008985
17  0.202327
18  0.152252
19  0.004041


我想在此格式中有数据框:

 var1 |  var2  |  var3  |  var4  |  var5  |  var6  |  var7  |var8
0.008985|0.486024|0.800637|0.006264|5.030551|0.001959|0.005949|0.002210
0.008985|0.689814|0.889638|0.007462|5.589760|0.005739|0.006474|0.004770


请建议如何在python中转换此数据框?

先感谢您

最佳答案

使用:

cols=['var'+str(i+1) for i in range(8)]
#['var1', 'var2', 'var3', 'var4', 'var5', 'var6', 'var7', 'var8']
df=pd.DataFrame(df.groupby(df.index//8)[1].apply(list).values.tolist(),columns=cols)
print(df)




       var1      var2      var3      var4      var5      var6      var7  \
0  0.008985  0.486024  0.800637  0.006264  5.030551  0.001959  0.002210
1  0.008985  0.689814  0.889638  0.007462  5.589760  0.005739  0.006474
2  0.008985  0.202327  0.152252  0.004041       NaN       NaN       NaN

       var8
0  0.005949
1  0.004770
2       NaN


注意用原始列名替换1

08-24 16:41