pandas 将具有多个值的行数据合并到列的Python列表中 [英] Pandas Merge row data with multiple values to Python list for a column
本文介绍了 pandas 将具有多个值的行数据合并到列的Python列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像
*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 的对应列表 所以最终输出应该是
I want a this dataframe to merge the rows where id are equal and have a list of Type corresponding list of URL so final output should be like
*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]
推荐答案
I think you need groupby
and aggregate tuple
and then convert to list
:
df = df.groupby(['id','name']).agg(lambda x: tuple(x)).applymap(list).reset_index()
print (df)
id name \
0 2 birth_france_by_region
1 3 long_lat
2 4 random_time_series
3 5 birth_names
URL Type
0 [http://abc.cm, http://pt.python] [T1, T2]
1 [http://abc.cm, http://pqur.com] [T3, T1]
2 [http://sadsdc.com, http://sadcadf.com] [T2, T3]
3 [http://google.;com, http://helloworld.com, ht... [T1, T2, T3]
由于版本0.20.3中出现错误:
Because in version 0.20.3 raise error:
df = df.groupby(['id','name']).agg(lambda x: x.tolist())
ValueError:函数不会减少
ValueError: Function does not reduce
这篇关于 pandas 将具有多个值的行数据合并到列的Python列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文