pandas groupby中的最大和最小日期

Dat*_*ede 25 python dataframe pandas

我有一个看起来像这样的数据框:

data = {'index': ['2014-06-22 10:46:00', '2014-06-24 19:52:00', '2014-06-25 17:02:00', '2014-06-25 17:55:00', '2014-07-02 11:36:00', '2014-07-06 12:40:00', '2014-07-05 12:46:00', '2014-07-27 15:12:00'],
    'type': ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'C'],
    'sum_col': [1, 2, 3, 1, 1, 3, 2, 1]}
df = pd.DataFrame(data, columns=['index', 'type', 'sum_col'])
df['index'] = pd.to_datetime(df['index'])
df = df.set_index('index')
df['weekofyear'] = df.index.weekofyear
df['date'] = df.index.date
df['date'] = pd.to_datetime(df['date'])



                     type sum_col weekofyear   date
index               
2014-06-22 10:46:00    A    1       25      2014-06-22
2014-06-24 19:52:00    B    2       26      2014-06-24
2014-06-25 17:02:00    C    3       26      2014-06-25
2014-06-25 17:55:00    A    1       26      2014-06-25
2014-07-02 11:36:00    B    1       27      2014-07-02
2014-07-06 12:40:00    C    3       27      2014-07-06
2014-07-05 12:46:00    A    2       27      2014-07-05
2014-07-27 15:12:00    C    1       30      2014-07-27
Run Code Online (Sandbox Code Playgroud)

我希望在一周的时间内分组,然后总结sum_col.另外,我需要找到本周最早和最新的日期.第一部分非常简单:

gb = df.groupby(['type', 'weekofyear'])
gb['sum_col'].agg({'sum_col' : np.sum})
Run Code Online (Sandbox Code Playgroud)

我试图找到这个最小/最大日期,但没有成功:

gb = df.groupby(['type', 'weekofyear'])
gb.agg({'sum_col' : np.sum,
        'date' : np.min,
        'date' : np.max})
Run Code Online (Sandbox Code Playgroud)

如何找到出现的最早/最新日期?

chr*_*isb 52

您需要组合应用于同一列的函数,如下所示:

In [116]: gb.agg({'sum_col' : np.sum,
     ...:         'date' : [np.min, np.max]})
Out[116]: 
                      date             sum_col
                      amin       amax      sum
type weekofyear                               
A    25         2014-06-22 2014-06-22        1
     26         2014-06-25 2014-06-25        1
     27         2014-07-05 2014-07-05        2
B    26         2014-06-24 2014-06-24        2
     27         2014-07-02 2014-07-02        1
C    26         2014-06-25 2014-06-25        3
     27         2014-07-06 2014-07-06        3
     30         2014-07-27 2014-07-27        1
Run Code Online (Sandbox Code Playgroud)

  • 要命名列,请提供字典,例如`gb.agg({'date':{'mindate':np.min,'maxdate':np.max}}) (12认同)
  • @connorbode:pandas v1.0.5 中似乎不支持嵌套列命名“pandas.core.base.SpecificationError:不支持嵌套重命名器” (2认同)
  • 新版本的 pandas 不支持“嵌套重命名器”。使用 `gb.date.agg(mindate=np.min, maxdate=np.max)` 代替。 (2认同)

Sha*_*ukh 17

简单的代码就可以

df.groupby([key_field]).agg({'time_field': [np.min,np.max]})
Run Code Online (Sandbox Code Playgroud)

其中 key_field 可以是 event_id,time_field 可以是时间戳字段。

  • 您还可以通过添加“df.groupby(['Month_Begin', 'Column2', 'Column3'],as_index=False).agg...”来保留标题。 (2认同)

Una*_*hez 6

另一种可能的解决方案,您可以更好地控制结果列名称:

gb = df.groupby(['type', 'weekofyear'])
gb.agg(
    sum_col=('sum_col', np.sum),
    first_date=('date', np.min),
    last_date=('date', np.max)
).reset_index()
Run Code Online (Sandbox Code Playgroud)