我有两个如下的Python系列。
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
我想按年份与月份的格式排列数据,如下所示:
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
需要解析应该具有该系列值的year,month。
答案 0 :(得分:3)
#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')