gpt4 book ai didi

python - 如何从包含年份和月份的单个日期列中为每年创建一个列?

转载 作者:太空狗 更新时间:2023-10-30 02:27:36 27 4
gpt4 key购买 nike

如果我有一个数据

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

谢谢。

最佳答案

您可以使用 splitpivot :

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

关于python - 如何从包含年份和月份的单个日期列中为每年创建一个列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40602960/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com