如何使用 id 连接数据帧行中的字符串?
How to concatenate strings in rows of dataframe usings ids?
这是我的数据集的示例。
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,则应将其连接成一行。以下是我的预期输出。
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)
我正在尝试像这样加入或连接。请帮忙!
res = pd.concat(df["sentences"], on=['Report id'])
将 groupby
与 apply
结合使用
例如:
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))
输出:
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
这是我的数据集的示例。
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,则应将其连接成一行。以下是我的预期输出。
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)
我正在尝试像这样加入或连接。请帮忙!
res = pd.concat(df["sentences"], on=['Report id'])
将 groupby
与 apply
例如:
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))
输出:
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