在python中根据月份与年份排列数据系列
我有两个Python系列,如下所示在python中根据月份与年份排列数据系列,python,python-3.x,pandas,pandas-groupby,Python,Python 3.x,Pandas,Pandas Groupby,我有两个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
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
需要解析年、月,其中应包含序列中的值。您需要
你需要
不错的一个,但是如果你看到OPs的预期输出,我想他需要
df['Year']=df.obstime.dt.strftime('%Y')
和df['Month']=df.obstime.dt.strftime('%b')
我看到obstime
是一列,那么我们需要df=df.set\u索引(obstime)
编辑了答案,请勾选nice one,但是如果你看到OPs的预期输出,我认为他需要df['Year']=df.obstime.dt.strftime('%Y')
和df['Month']=df.obstime.dt.strftime('%b')
在编辑之前,我在原始问题中将obstime
作为索引,如果obstime
是一列,然后我们需要df=df。设置索引(obstime)
编辑答案,请检查
#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')