一次在多列上使用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.
df.groupby('a')['b'].apply(list)
如果只需要将 1 列(在本例中为 'b')添加到列表中,则效果很好,但我不知道如何为多列做这件事.
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]
有没有简单的方法可以做到这一点?
Is there an easy way to do this?
推荐答案
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屋!
查看全文