Pandas,将多行转换为多列

时间:2018-06-11 08:10:30

标签: python pandas dataframe

我有一个示例数据

Index, Column1,Value1
1,  1,6
1,  2,7
1,  3,8
Index,  Column1,Value1
2,  11,16
2,  12,17
2,  13,18
2,  14,19
2,  15,20
Index,  Column1,Value1
3,  21,24
3,  22,25

我想以这种方式重塑

Index Column1, Value1, Column2, Value2, Column3, Value3, Column4, Value4
1   1,  6,  2,  7,  3,  8
2   11, 16, 12, 17, 13, 18, 14, 19
3   21, 24, 22, 25  

我正在使用此

print(df.groupby('Index')[['Column1', 'Value1']].apply(','.join).reset_index())

但我得到了奇怪的输出。我在这做错了什么? 有什么建议 ?

Index               0
    1  Column1,Value1
    2  Column1,Value1
    3  Column1,Value1
Index  Column1,Value1

1 个答案:

答案 0 :(得分:1)

编辑:

3列输入DataFrame的解决方案:

df = df[df['Column1'] != 'Column1']
s =  df.groupby('Index').cumcount().add(1)
df = (df.set_index(['Index',s])
        .unstack()
        .sort_index(axis=1, level=1)
       )
df.columns = ['{}_{}'.format(a, b) for a,b in df.columns]

df = df.reset_index()
print (df)
  Index Column1_1 Value1_1 Column1_2 Value1_2 Column1_3 Value1_3 Column1_4  \
0     1         1        6         2        7         3        8       NaN   
1     2        11       16        12       17        13       18        14   
2     3        21       24        22       25       NaN      NaN       NaN   

  Value1_4 Column1_5 Value1_5  
0      NaN       NaN      NaN  
1       19        15       20  
2      NaN       NaN      NaN