python-3.x 如何在panda的字符串列表中分组?

eagi6jfj  于 2022-12-24  发布在  Python
关注(0)|答案(1)|浏览(120)

我的数据

Column 1       Column 2                              Column 3
"Task 1"       ["emailofowner1","emailofowner2"]      John Doe
"Task 37"      ["emailofowner1","emailofowner2"]      John Doe

我有许多这样的行,我希望我的输出是:

Column 1                 Column 2                            Column 3
["Task1","Task37"]       ["emailofowner1","emailofowner2"]     John Doe
ru9i0ody

ru9i0ody1#

groupby需要可哈希对象,而list不需要。
您可以转换为元组以用作grouper:

out = (df
  .groupby(df['Column 2'].apply(tuple), as_index=False)
  .agg({'Column 1': list, 'Column 2': 'first', 'Column 3': 'first'})
)

输出:

Column 1                        Column 2  Column 3
0  [Task 1, Task 37]  [emailofowner1, emailofowner2]  John Doe

相关问题