Dhiraj Kumar
Dhiraj Kumar

Reputation: 93

Arranging the data-series according to month vs year in python

I have two Python series like below.

obstime        temperature

2012-01-31   -10.203452
2012-02-29    -7.818472
2012-03-31   -10.965704
2012-04-30   -12.800104
2012-05-31   -16.666207
2012-06-30   -11.511220
2012-07-31   -17.928276
2012-08-31   -14.837011
2012-09-30   -13.116554
2012-10-31    -9.929026
2012-11-30    -5.082396
2012-12-31   -10.915046
2013-01-31   -15.459292
2013-02-28    -8.278767
2013-03-31   -13.764899
2013-04-30   -13.262068
2013-05-31   -15.787945
2013-06-30   -13.096949
2013-07-31   -15.841149
2013-08-31   -16.051178
...
2016-01-31    -4.883573

And I want to arrange the data in year vs month format like shown below:

Year    Jan   Feb   Mar   Apr    May   Jun   Jul   Aug   Sep   Oct  Nov  Dec
2012   -10.20 -7.81  ......
2013   -15.45  -8.27....
...
2016   -4.88  -7.94

Need to parse the year , month which should have the values from the series.

Upvotes: 0

Views: 52

Answers (1)

Pyd
Pyd

Reputation: 6159

you need df.pivot_table

#if obstime is one of the columns, then convert it to index
#df.set_index('obstime',inplace=True)
#make your index to datetime
df.index=pd.to_datetime(df.index)

df['Year']=df.index.year

df['Month']=df.index.strftime('%b')

df.pivot_table(columns='Month',index='Year',values='temperature')

Upvotes: 2

Related Questions