一次在多个列上使用pandas groupby().apply(list) [英] Using pandas groupby().apply(list) on multiple columns at once
本文介绍了一次在多个列上使用pandas groupby().apply(list)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将一个数据帧的多行合并为一行,将具有不同值的列合并到一个列表中.有多个具有不同值的列.
I'm trying to combine multiple rows of a dataframe into one row, with the columns with different values being combined in a list. There are multiple columns with different values.
如果只需要对列表进行1列(在本例中为'b'),则 df.groupby('a')['b'].apply(list)
效果很好,但我不知道如何针对多列进行该操作.
The df.groupby('a')['b'].apply(list)
works well if only 1 column ('b' in this instance) has to be made to a list, but I can't figure out how to do it for multiple columns.
数据框:
a b c d
0 1 b 1 first
1 1 b 2 second
2 2 c 1 third
3 2 c 2 fourth
4 2 c 3 fifth
首选数据帧后操作:
a b c d
0 1 b [1, 2] [first, second]
1 2 c [1, 2, 3] [third, fourth, fifth]
有一种简单的方法吗?
推荐答案
df = df.groupby(['a','b']).apply(lambda x: [list(x['c']), list(x['d'])]).apply(pd.Series)
df.columns =['a','b','c','d']
输出
a b c d
0 1 b [1, 2] [first, second]
1 2 c [1, 2, 3] [third, fourth, fifth]
这篇关于一次在多个列上使用pandas groupby().apply(list)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文