如何在pandas中按行连接列表类型列?例如,见下文-
以前,
1 a [a,b,c]
1 b [a,d]
之后,
1 b [a,b,c,d]
我做了如下列式列表连接
df['all_poi'] = df['poi_part1'] + df['poi_part2']
电流输出
location_id city all_poi
6265981 Port Severn [Mount St. Louis Moonstone , Horseshoe Valley , Lake Muskoka]
6265981 Port Severn [Mount St. Louis Moonstone , Little Lake Park , Bamboo Spa , Lake Huron]
预期产出
location_id city all_poi
6265981 Port Severn [Mount St. Louis Moonstone , Horseshoe Valley , Lake Muskoka, Little Lake Park , Bamboo Spa , Lake Huron]
检查基于位置id合并的所有poi值
您可以在^{} 中的自定义函数中创建集合:
如果顺序和性能很重要,请使用
dict
删除重复项:另一个想法是使用
unique
:编辑:
如果有多个列并且每个组需要第一个值:
如果需要其他一些聚合方法,如
sum
、mean
、join
:简单的
sum()
怎么样:产出:
看起来下面的答案更简洁,但是您可以将
sum
与groupby一起应用来组合列表。然后创建一个集合以消除重复项,并从set
转换为list
相关问题 更多 >
编程相关推荐