簡體   English   中英

如何從包含年份和月份的單個日期列創建每年的列?

[英]How to create a column for each year from a single date column containing year and month?

如果我有數據

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

謝謝。

您可以使用split with pivot

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM