将数据框的多行折叠为一行-基于唯一键 [英] Collapse mutiple rows of a dataframe into one row - based on a unique key
问题描述
我的数据框为:
1 A1
1 A11
2 A2
2 A22
2 A23
3 A3
3 A33
4 A4
4 A44
4 A444
5 A5
我需要的是:-
1 | A1, A11
2 | A2, A22, A23
3 | A3, A33
4 | A4, A44, A444
5 | A5
即.每列可以有不同数量的行.
ie. each column can have different number of rows present.
无论如何,我可以优雅地折叠它们,而无需使用字典中的读数,然后将其合并到适用的列表中.在传统意义上,我需要对此执行多个连接-可以吗?
Anyway I can collapse them elegantly, without using the reading from dict and then concat to the list as applicable. In the traditional sense I need to perform multiple joins on this - Any way around ?
请注意,最后只能有2列.
Note that there should be only 2 final columns.
推荐答案
df =pd.DataFrame({'A':[1,1,1,2,2,3,3,3], 'B':['aaa','bbb','cc','gg','aaa','bbb','cc','gg']})
def f(x):
return [x['B'].values]
df.groupby('A').apply(f)
在要减少的列上创建一个分组依据,然后应用一个函数,该函数按每个分组的列表返回该分组的结果.请注意,这将返回一个序列.
Create a group by on the column you want to reduce over and then apply a function that returns the results of the group by an a list per group. Note this returns a series.
更新:将系列更改为数据框.
Update: change the series to a dataframe.
series =df.groupby('A').apply(f)
series.name = 'metric'
series.reset_index()
这篇关于将数据框的多行折叠为一行-基于唯一键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!