简体   繁体   中英

condense multiple rows into 1 aggregated by other columns pandas

How do I select a column that is grouped by other columns and put all the values of the column in 1 row (in a list). Input:

PropID    Manager    Manager_email      date
  1         A         a@aol.com       2021-02-01
  1         A         a@aol.com       2021-02-02
  1         A         a@aol.com       2021-02-03
  1         A         a@aol.com       2021-02-04
  2         B         b@aol.com       2021-02-08
  2         B         b@aol.com       2021-02-10
  2         B         b@aol.com       2021-02-12
  2         B         b@aol.com       2021-02-16

Desired Output:

PropID    Manager    Manager_email                      date
  1         A         a@aol.com       [2021-02-01,2021-02-02,2021-02-03,2021-02-04]
  2         B         b@aol.com       [2021-02-08,2021-02-10,2021-02-12,2021-02-16]

试试这个:

df.groupby(['PropID','Manager','Manager_email'])['date'].agg(list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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