使用pandas根据值按日期分组数据

2024-04-18 13:31:25 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用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

谢谢


Tags: 数据pandasfirstlast月份
2条回答

试试这个:

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

minmax取决于您想要什么:

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

使用idxminidxmax标识要为其获取适当行的索引。你知道吗

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

相关问题 更多 >