按 id 合并行 [英] Combine rows by id
本文介绍了按 id 合并行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下类型的数据.如何按id合并行,类型记录单独列(所有类型10)(或记录类型,单列用逗号分隔)
I have the following types of data. How to combine rows by id, and the type record a separate column (all types 10) (or record types, separated by commas in a single column)
id,type,value
1,8,value1
1,2,value1
1,7,value1
1,3,value1
1,10,value1
2,3,value1
2,8,value1
2,7,value1
所需的输出:
id type value
0 1 8,2,7,3,10 value1
1 2 3,8,7 value1
推荐答案
我认为你可以使用 groupby
with apply
join
,但先转换int
到 str
:
I think you can use groupby
with apply
join
, but first convert int
to str
:
df = df.groupby('id')['type'].apply(lambda x: ','.join(x.astype(str))).reset_index()
print (df)
id type
0 1 8,2,7,3,10
1 2 3,8,7
这篇关于按 id 合并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文