我有一个像下面的示例数据框
df1 = pd.DataFrame({'Gender':['Male','Male','Male','Male','Female','Female','Female','Female','Male','Male','Male','Male','Female','Female','Female','Female'],
'Year' :[2008,2008,2009,2009,2008,2008,2009,2009,2008,2008,2009,2009,2008,2008,2009,2009],
'rate':[2.3,3.2,4.5,6.7,5.6,3.2,3.5,2.6,2.3,3.2,4.5,6.7,5.6,3.2,3.5,2.6],
'Heading':['TNMAB123','TNMAB123','TNMAB123','TNMAB123','TNMAB123','TNMAB123','TNMAB123','TNMAB123',
'TNMAB456','TNMAB456','TNMAB456','TNMAB456','TNMAB456','TNMAB456','TNMAB456','TNMAB456'],
'target':[31.2,33.4,33.4,35.2,35.2,36.4,36.4,37.2,31.2,33.4,33.4,35.2,35.2,36.4,36.4,37.2],
'day_type':['wk','wkend','wk','wkend','wk','wkend','wk','wkend','wk','wkend','wk','wkend','wk','wkend','wk','wkend']})
我想对它们进行转置/旋转以得到如下所示的输出,但是对于我的代码,它会抛出如下所示的错误
df1.pivot(index='Year', columns='Heading', values='rate')
我在SO帖子的帮助下写了这篇文章,但是对于3列,我不确定如何使它起作用?
df1 = df1.pivot_table(index=['Year','Gender','day_type'],columns='Heading',values='rate').unstack()
df1.columns = ['_'.join(i) for i in df1.columns.tolist()]
我希望我的输出如下图所示,其中每年作为一行,而该年的所有相应条目作为列。
请注意,由于表列的结构更重要,因此我没有填写这些值。
答案 0 :(得分:1)
尝试使用map
,也需要unstack
两个level
df1 = df1.pivot_table(index=['Year','Gender','day_type'],columns='Heading',values='rate').unstack([1,2])
df1.columns=df1.columns.map('_'.join)
df1
TNMAB123_Female_wk ... TNMAB456_Male_wkend
Year ...
2008 5.6 ... 3.2
2009 3.5 ... 6.7
[2 rows x 8 columns]