2 回答
TA贡献1828条经验 获得超3个赞
检查与melt
out = df.melt(['Product','Year'], var_name = 'Period', value_name = 'Sales')
Product Year Period Sales
0 1 2000 Jan-Feb 2
1 1 2001 Jan-Feb 4
2 2 2001 Jan-Feb 2
3 2 2002 Jan-Feb 4
4 1 2000 Mar-Apr 1
5 1 2001 Mar-Apr 2
6 2 2001 Mar-Apr 1
7 2 2002 Mar-Apr 6
TA贡献1898条经验 获得超8个赞
用途stack():
df = df.set_index(['Product','Year']).stack(0).reset_index()
df.columns = ['Product','Year','Period','Sales']
输出:
Product Year Period Sales
0 1 2000 Jan-Feb 2
1 1 2000 Mar-Apr 1
2 1 2001 Jan-Feb 4
3 1 2001 Mar-Apr 2
4 2 2001 Jan-Feb 2
5 2 2001 Mar-Apr 1
6 2 2002 Jan-Feb 4
7 2 2002 Mar-Apr 6
添加回答
举报