如何从包含年和月的单个日期列创建每年的列?

2024-04-25 13:39:42 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有数据

Date        Values
2005-01     10
2005-02     20
2005-03     30
2006-01     40
2006-02     50
2006-03     70

如何更改年份栏?像这样

Date 2015 2016
 01   10   40
 02   20   50
 03   30   70

谢谢。你知道吗


Tags: 数据datevalues年份
1条回答
网友
1楼 · 发布于 2024-04-25 13:39:42

可以将^{}^{}一起使用:

df[['year','month']] = df.Date.str.split('-', expand=True)
df = df.pivot(index='month', columns='year', values='Values')
print (df)
year   2005  2006
month            
01       10    40
02       20    50
03       30    70

相关问题 更多 >