Python 使用pandas根据值按日期对数据分组
如何使用pandas将以下数据按月分组:Python 使用pandas根据值按日期对数据分组,python,python-2.7,pandas,dataframe,Python,Python 2.7,Pandas,Dataframe,如何使用pandas将以下数据按月分组: 17/1/2001 800 7/1/2001 1300 2/1/2001 400 1/1/2001 200 25/3/2001 1800 8/3/2001 1300 然后,将该月的第一天和最后一天以及相应的第一天和最后一天值输出如下: First Last First Last 1/1/2001 17/1/2001 200 800 8/3/2001 25/3/2001 1300 1800 谢谢试试这个
17/1/2001 800
7/1/2001 1300
2/1/2001 400
1/1/2001 200
25/3/2001 1800
8/3/2001 1300
然后,将该月的第一天和最后一天以及相应的第一天和最后一天值输出如下:
First Last First Last
1/1/2001 17/1/2001 200 800
8/3/2001 25/3/2001 1300 1800
谢谢试试这个:
In [102]: res = df.sort_values('date').groupby(df.date.dt.month).agg(['first','last'])
In [104]: res.columns = ['date_first', 'date_last', 'first', 'last']
In [105]: res
Out[105]:
date_first date_last first last
date
1 2001-01-01 2001-01-17 200 800
3 2001-03-08 2001-03-25 1300 1800
或min
,max
,具体取决于您需要什么:
In [95]: res = df.groupby(df.date.dt.month).agg(['min','max'])
In [96]: res.columns = ['date_min', 'date_max', 'min', 'max']
In [97]: res
Out[97]:
date_min date_max min max
date
1 2001-01-01 2001-01-17 200 1300
3 2001-03-08 2001-03-25 1300 1800
使用
idxmin
和idxmax
标识要获取相应行的索引
def get_min(x):
return x.loc[x.date.idxmin(), :]
def get_max(x):
return x.loc[x.date.idxmax(), :]
def app_by_month(df, f):
return df.groupby(df.date.dt.month).apply(f)
df2 = pd.concat([app_by_month(df, f) for f in [get_min, get_max]],
axis=1, keys=['first', 'last']).sort_index(axis=1, level=1)
df2.columns = df2.columns.to_series().str.join('_').values
print df2
first_date last_date first_value last_value
date
1 2001-01-01 2001-01-17 200 800
3 2001-03-08 2001-03-25 1300 1800
你的解决方案是不同的谢谢这么多人。对不起,我离开太久了。