简体   繁体   English

Pandas 在 groupby 之后添加带有列表的计数和列

[英]Pandas add count and column with list after groupby

I have the following dataframe df :我有以下 dataframe df

Month       Cat Constr  Part
1/1/2021    asd v1      p1
1/1/2021    asd v1      p2
1/1/2021    asd v2      p1
1/1/2021    pqr v1      p1
1/1/2021    pqr v1      p2
1/1/2021    pqr v2      p2

I am trying to achieve the following:我正在努力实现以下目标:

Month       Cat Constr  count   part
1/1/2021    asd v1      2       [p1, p2]
1/1/2021    asd v2      1       [p1]
1/1/2021    pqr v1      2       [p1,p2]
1/1/2021    pqr v2      1       [p2]

To get the count column I do the following:要获取计数列,我执行以下操作:

df.groupby(['Month', 'Cat', 'Constr']).agg(['count'])

But I am not sure how to add the list of Part who are part of groupby但我不确定如何添加属于groupbyPart列表

I am trying to do group by ['Month', 'Cat', 'Constr'] and find the number of Part for each groupby unique combination and a list of Part which form that group.我正在尝试按['Month', 'Cat', 'Constr']进行分组,并根据唯一组合和构成该组的Part Part

Use groupby as you did but apply list :像你一样使用groupby但应用list

>>> df.groupby(['Month', 'Cat Constr']).agg(list).reset_index()

       Month Cat Constr      Part
0 2021-01-01     asd v1  [p1, p2]
1 2021-01-01     asd v2      [p1]
2 2021-01-01     pqr v1  [p1, p2]
3 2021-01-01     pqr v2      [p2]

To get all together:聚在一起:

>>> df.groupby(['Month', 'Cat Constr'])['Part'] \
      .agg(count='count', part=list)

       Month Cat Constr  count      part
0 2021-01-01     asd v1      2  [p1, p2]
1 2021-01-01     asd v2      1      [p1]
2 2021-01-01     pqr v1      2  [p1, p2]
3 2021-01-01     pqr v2      1      [p2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM