我在一个框架中有如下数据:
Region Date Drip Coffee Espresso Latte Other
Central 1 5 1 2 3
East 1 3 3 1 4
North 1 5 1 3 2
Central 2 2 7 2 0
East 2 10 3 2 1
North 2 6 9 4 2
.
.
.
我想把咖啡、意式浓缩咖啡、拿铁咖啡和其他咖啡放在一起,这样它就可以在日期和地区重复排列,比如:
Region Date Type Value
Central 1 Drip Coffee 5
East 1 Drip Coffee 3
North 1 Drip Coffee 5
Central 1 Espresso 1
East 1 Espresso 3
North 1 Espresso 1
.
.
.
Central 2 Drip Coffee 2
East 2 Drip Coffee 10
North 2 Drip Coffee 6
.
.
我试过一些方法,比如:
df_new = df_old.pivot(index='Date',columns=['Drip Coffee', 'Espresso', 'Latte', 'Other']).stack(0).rename_axis(['Date','Type']).reset_index()
但这给了我ValueError: all arrays must be same length
我知道在我的Value
测试中缺少了一个新的列,但这是因为我不知道如何像这样透视一系列值
我想看看是否有一个可能的解决办法,因为这个问题似乎是非常独特的;我找不到这样一个多重重复的解决方案
设置
IIUC,使用
melt
和sort_values
相关问题 更多 >
编程相关推荐