[英]Group dataframe columns in lists
I have a dataframe which looks like this:我有一个看起来像这样的数据框:
| id | A | B | C | D |
| 1 | 50 | 51 | 52 | 53 |
| 2 | 70 | 71 | 72 | 73 |
| 1 | 80 | 81 | 82 | 83 |
| 1 | 90 | 91 | 92 | 93 |
| 2 | 40 | 41 | 42 | 43 |
I want to group it on 'id' column so that each row is in form of list.我想将它分组在“id”列上,以便每一行都采用列表的形式。
Expected Output:预期输出:
| id | A | B | C | D |
| 1 | [50,80,90] | [51,81,91] | [52,82,92] | [53,83,93] |
| 2 | [70,40] | [71,41] | [72,42] | [73,43] |
Explaination: The values for id 1 in column A are all in one list similarly for other.说明:A 列中 id 1 的值都在一个列表中,其他列表类似。 The length of list depends on number of records of that id in initial dataframe.
列表的长度取决于初始数据帧中该 id 的记录数。
My approach:我的做法:
df_grouped = df.groupby(['id'])['A'].apply(lambda x: list(x)).reset_index().merge(df.groupby(['id'])['B'].apply(lambda x: list(x)).reset_index().merge(df.groupby(['id'])['C'].apply(lambda x: list(x)).reset_index().merge(df.groupby(['id'])['D'].apply(lambda x: list(x)).reset_index()),on=['id'],how='left'))
Although this gives me the desired output, but its slow for large dataframes and I feel this is not very optimal as well as we are grouping on id each time and merging.虽然这给了我想要的输出,但是对于大型数据帧来说它很慢,我觉得这不是很理想,因为我们每次都在 id 上分组并合并。 There should be a way wherein I group on id once and do something columns.tolist() and it gives the same output.
应该有一种方法,我将 id 分组一次并做一些 columns.tolist() 并且它给出相同的输出。
Any help would be appreciated.任何帮助,将不胜感激。 Thanks
谢谢
Use GroupBy.agg
:使用
GroupBy.agg
:
#all columns without id
df_grouped = df.groupby('id').agg(list).reset_index()
Or:或者:
#columns specified in list
df_grouped = df.groupby('id')[['A','B','C','D']].agg(list).reset_index()
print (df_grouped)
id A B C D
0 1 [50, 80, 90] [51, 81, 91] [52, 82, 92] [53, 83, 93]
1 2 [70, 40] [71, 41] [72, 42] [73, 43]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.