如何在python中将单行拆分为具有特定列号的多列?

时间:2019-04-29 11:33:11

标签: python pandas dataframe arraylist

如下所述,我在一行中有数据,但是我想将行转换为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中转换此数据框?

先谢谢您

3 个答案:

答案 0 :(得分:1)

使用:

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

答案 1 :(得分:1)

您可以将数据转换为numpy数组并对其进行整形。然后从该DataFrame数组中创建一个numpy

import pandas as pd
import numpy as np

n = 20
df = pd.DataFrame({'row': range(n)})
df = pd.DataFrame(np.array(df.row).reshape(2, -1))
df.columns = [f'var{i}' for i in range(1, n//2 + 1)]

答案 2 :(得分:1)

使用numpy解决方案-更改了unutbu的答案-初始化由NaN填充的空数组:

arr = np.full(((len(df) - 1)//8 + 1)*8, np.nan)
arr[:len(df)] = df['col']
df = pd.DataFrame(arr.reshape((-1, 8)), columns=[f'var{i}' for i in range(1,9)])
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