首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将字符串连接在使用ids的数据行中?

如何将字符串连接在使用ids的数据行中?
EN

Stack Overflow用户
提问于 2019-09-30 09:26:12
回答 1查看 267关注 0票数 2

这里是我的数据集的一个例子。

代码语言:javascript
复制
d={'Report id': [0, 0, 1, 1], 'sentences': ['There is also a faint ground glass nodule. ', 'Other two ill  defined, small ground glass lesions are seen.', 'There is a small nodule at medial aspect of left breast, measured 11 mm in size.', 'Two heterogeneous enhancing lesions at lateral segment of left lobe']}
df1 = pd.DataFrame(data=d)

我想根据从0开始的报表id连接数据帧的行。如果行具有相同的报告id,则应将其连接到一行中。以下是我的预期输出。

代码语言:javascript
复制
dd = {'Report id': [0, 1], 'sentences': ['There is also a faint ground glass nodule. ' 'Other two ill  defined, small ground glass lesions are seen.', 'There is a small nodule at medial aspect of left breast, measured 11 mm in size.' 'Two heterogeneous enhancing lesions at lateral segment of left lobe']}
df2 = pd.DataFrame(data=dd)

我试着加入或者像这样连接。请帮帮我!

代码语言:javascript
复制
res = pd.concat(df["sentences"], on=['Report id'])
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-30 09:27:58

groupbyapply结合使用

Ex:

代码语言:javascript
复制
d={'Report id': [0, 0, 1, 1], 'sentences': ['There is also a faint ground glass nodule. ', 'Other two ill  defined, small ground glass lesions are seen.', 'There is a small nodule at medial aspect of left breast, measured 11 mm in size.', 'Two heterogeneous enhancing lesions at lateral segment of left lobe']}
df1 = pd.DataFrame(data=d)
print(df1.groupby('Report id')['sentences'].apply(" ".join))

输出:

代码语言:javascript
复制
Report id
0    There is also a faint ground glass nodule.  Ot...
1    There is a small nodule at medial aspect of le...
Name: sentences, dtype: object
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58164994

复制
相关文章

相似问题

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