【发布时间】:2018-02-26 18:46:49
【问题描述】:
我有一个看起来像
的数据框数据
*id*, *name*, *URL*, *Type*
2, birth_france_by_region, http://abc. com, T1
2, birth_france_by_region, http://pt. python, T2
3, long_lat, http://abc. com, T3
3, long_lat, http://pqur. com, T1
4, random_time_series, http://sadsdc. com, T2
4, random_time_series, http://sadcadf. com, T3
5, birth_names, http://google. com, T1
5, birth_names, http://helloworld. com,T2
5, birth_names, http://hu. com, T3
我想要一个这个数据框来合并 id 相等的行,并有一个 Type 列表对应的 URL 列表 所以最终输出应该是这样的
*id*, *name*, *URL*, *Type*
2,birth_france_by_region, [http://abc .com,http://pt.python], [T1,T2]
3,long_lat, [http://abc .com,http://pqur. com], [T3,T1]
4,random_time_series, [http://sadsdc. com,http://sadcadf .com,],[T2,T3]
5,birth_names, [http://google .com,http://helloworld. com,
http://hu. com] , [T1,T2,T3]
【问题讨论】:
-
This 问题解决了数据框只有两列的情况。答案中有一个警告,类似于此处接受的解决方案(最简单的形式:
df.groupby['id'].agg(list))存在巨大的性能问题。
标签: python list pandas dataframe merge