使用列值列表对 pandas 中的各个列进行分组

Group various columns in pandas with list of column values

我有一个示例数据:

date       Product Advert_Type  total_clients
2020-01-01.  Dell.    call.       10
2020-01-01.  Dell.    Email.      5
2020-01-01.  Apple.   call.       6
2020-01-01.  Apple    fax.        4
2020-01-02.  Dell.    Email.      5
2020-01-02.  Dell.    fax.        4
2020-01-02.  Apple.   visit.      2
2020-01-02.  Apple.   call.       1

我想获得每种产品每月获得的客户总数以及每种产品类型在该月完成的事件列表。

输出应如下所示:

date       Product. Advert_Type.   Total_Clients
2020-01-01.  Dell.  [call,email].    15
2020-01-01.  Apple.  [call, fax].    10
2020-01-02.  Dell   [email, fax].     9
2020-01-02   Apple.  [visit, call].   3

您可以使用 groupby:

df = df.groupby(['date', 'Product']).agg(
    {'Advert_Type': list, 'total_clients': sum}).reset_index()

输出

          date Product      Advert_Type  total_clients
0  2020-01-01.  Apple.    [call., fax.]             10
1  2020-01-01.   Dell.  [call., Email.]             15
2  2020-01-02.  Apple.  [visit., call.]              3
3  2020-01-02.   Dell.   [Email., fax.]              9