我有以下格式的数据:
Age Gender Period Unemployed
16 to 19 years Men 2005-01-01 91000
20 to 24 years Men 2005-01-01 175000
16 to 19 years Women 2005-01-01 88888
20 to 24 years Women 2005-01-01 175000
16 to 19 years Men 2005-02-01 92000
20 to 24 years Men 2005-02-01 175000
现在我想将其转换为年龄索引等宽格式:
2005-01-01 2005-02-01
Men Women Men Women
Age
16 to 19 years
20 to 24 years
我尝试使用数据透视:
df.pivot(index="Age", columns=["Period", "Gender"], value="Unemployed")
但是我得到关于重复值的错误。但是我不知道为什么这里不存在重复值的限制,因为我不聚合任何东西只是为了重塑?
答案 0 :(得分:1)
使用pivot_table
将Period
和Gender
都设置为列:
df.pivot_table(index='Age', columns=['Period' ,'Gender'], values='Unemployed')
Period 2005-01-01 2005-02-01
Gender Men Women Men
Age
16 to 19 years 91000 88888 92000
20 to 24 years 175000 175000 175000