首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >熊猫将输出转换为逗号单独的字符串,而不是列表

熊猫将输出转换为逗号单独的字符串,而不是列表
EN

Stack Overflow用户
提问于 2018-05-16 20:12:40
回答 3查看 1.2K关注 0票数 2

我有一个看起来像这样的DataFrame:

代码语言:javascript
复制
df = pd.DataFrame({'ID':[1,1,2,2,3,4],'Name':['John Doe','Jane Doe','John Smith','Jane Smith','Jack Hill','Jill Hill']})

    ID  Name
0   1   John Doe
1   1   Jane Doe
2   2   John Smith
3   2   Jane Smith
4   3   Jack Hill
5   4   Jill Hill

然后,我按ID添加了另一列分组,并取名称中的唯一值:

代码语言:javascript
复制
df['Multi Name'] = df.groupby('ID')['Name'].transform('unique')

    ID  Name    Multi Name
0   1   John Doe    [John Doe, Jane Doe]
1   1   Jane Doe    [John Doe, Jane Doe]
2   2   John Smith  [John Smith, Jane Smith]
3   2   Jane Smith  [John Smith, Jane Smith]
4   3   Jack Hill   [Jack Hill]
5   4   Jill Hill   [Jill Hill]

如何从多个名称中删除括号?

我试过:

代码语言:javascript
复制
df['Multi Name'] = df['Multi Name'].str.strip('[]')


ID  Name    Multi Name
0   1   John Doe    NaN
1   1   Jane Doe    NaN
2   2   John Smith  NaN
3   2   Jane Smith  NaN
4   3   Jack Hill   NaN
5   4   Jill Hill   NaN

期望产出:

代码语言:javascript
复制
    ID  Name    Multi Name
0   1   John Doe    John Doe, Jane Doe
1   1   Jane Doe    John Doe, Jane Doe
2   2   John Smith  John Smith, Jane Smith
3   2   Jane Smith  John Smith, Jane Smith
4   3   Jack Hill   Jack Hill
5   4   Jill Hill   Jill Hill
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-16 20:14:51

transform

代码语言:javascript
复制
df.join(df.groupby('ID').Name.transform('unique').rename('Multi Name'))

   ID        Name                Multi Name
0   1    John Doe      [John Doe, Jane Doe]
1   1    Jane Doe      [John Doe, Jane Doe]
2   2  John Smith  [John Smith, Jane Smith]
3   2  Jane Smith  [John Smith, Jane Smith]
4   3   Jack Hill               [Jack Hill]
5   4   Jill Hill               [Jill Hill]
代码语言:javascript
复制
df.join(df.groupby('ID').Name.transform('unique').str.join(', ').rename('Multi Name'))

   ID        Name              Multi Name
0   1    John Doe      John Doe, Jane Doe
1   1    Jane Doe      John Doe, Jane Doe
2   2  John Smith  John Smith, Jane Smith
3   2  Jane Smith  John Smith, Jane Smith
4   3   Jack Hill               Jack Hill
5   4   Jill Hill               Jill Hill

map

代码语言:javascript
复制
df.join(df.ID.map(df.groupby('ID').Name.unique().str.join(', ')).rename('Multi Name'))

   ID        Name              Multi Name
0   1    John Doe      John Doe, Jane Doe
1   1    Jane Doe      John Doe, Jane Doe
2   2  John Smith  John Smith, Jane Smith
3   2  Jane Smith  John Smith, Jane Smith
4   3   Jack Hill               Jack Hill
5   4   Jill Hill               Jill Hill

itertools.groupby

代码语言:javascript
复制
from itertools import groupby

d = {
    k: ', '.join(x[1] for x in v)
    for k, v in groupby(sorted(set(zip(df.ID, df.Name))), key=lambda x: x[0])
}

df.join(df.ID.map(d).rename('Multi Name'))

   ID        Name              Multi Name
0   1    John Doe      Jane Doe, John Doe
1   1    Jane Doe      Jane Doe, John Doe
2   2  John Smith  Jane Smith, John Smith
3   2  Jane Smith  Jane Smith, John Smith
4   3   Jack Hill               Jack Hill
5   4   Jill Hill               Jill Hill
票数 5
EN

Stack Overflow用户

发布于 2018-05-16 20:13:58

在这里,unique似乎是函数的错误选择。我推荐一个使用str.join的定制lambda函数

代码语言:javascript
复制
df['Multi Name'] = df.groupby('ID')['Name'].transform(lambda x: ', '.join(set(x)))

代码语言:javascript
复制
df
   ID        Name              Multi Name
0   1    John Doe      John Doe, Jane Doe
1   1    Jane Doe      John Doe, Jane Doe
2   2  John Smith  Jane Smith, John Smith
3   2  Jane Smith  Jane Smith, John Smith
4   3   Jack Hill               Jack Hill
5   4   Jill Hill               Jill Hill
票数 5
EN

Stack Overflow用户

发布于 2018-05-16 20:17:09

使用mapjoin

代码语言:javascript
复制
df['Multi Name'] = df.groupby('ID')['Name'].transform('unique').map(', '.join)

输出:

代码语言:javascript
复制
   ID        Name              Multi Name
0   1    John Doe      John Doe, Jane Doe
1   1    Jane Doe      John Doe, Jane Doe
2   2  John Smith  John Smith, Jane Smith
3   2  Jane Smith  John Smith, Jane Smith
4   3   Jack Hill               Jack Hill
5   4   Jill Hill               Jill Hill
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50379203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档