GrayHash
GrayHash

Reputation: 289

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

If I have a Data

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

How can I change Year Column? like this

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

Thanks.

Upvotes: 4

Views: 88

Answers (1)

jezrael
jezrael

Reputation: 862671

You can use 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

Upvotes: 6

Related Questions