带有agg的pandas groupby无法处理多列 [英] pandas groupby with agg not working on multiple columns
问题描述
我正在尝试将多个列合并到一个基于 Pandas 分组的列表中.下面是我正在使用的代码
I'm trying to merge multiple columns, each into a list based on a group by in pandas. Below is the code I'm using
grouped_df = df.groupby(['d_id', 'time']).agg({'d_name': lambda x: tuple(x)},
{'ver': lambda x: tuple(x)},
{'f_name': lambda x: tuple(x)})
这只会给我一个列表中的第一列 (d_name),d_id 和时间在 grouped_df 中.其他两列不显示为列表.我之前尝试使用 list 但发现 list 有 agg 函数的问题,所以我求助于元组.如果我在这里做错了什么,请告诉我.
This only gives me the first column (d_name) in a list with d_id and time in grouped_df. The other two columns do not show as lists. I tried using list earlier but found out that list has an issue with agg function so I resorted to tuple. Let me know if I'm doing something wrong here.
感谢 RafaelC 对此的回答.现在我试图将这些列表列作为 grouped_df 添加到原始数据框中.当我看到 grouped_df 中的列时,它们显示为
Thanks to RafaelC for the answer to this. Now I am trying to add these list columns to the original dataframe as grouped_df. When I see the columns in grouped_df they come out as
Index([u'ver', u'f_name', u'd_name'], dtype='object')
但是当我做头像时,我得到了
But when I do a head, I get
ver \
d_id time
1 2018-06-01 (ver1, ver2, ver3.....
2 2018-06-01 (ver1, ver2, ver3.....
3 2018-06-01 (ver1, ver2, ver3.....
f_name \
d_id time
1 2018-06-01 (f_name1, f_name2, f_name2.....
2 2018-06-01 (f_name1, f_name2, f_name2.....
3 2018-06-01 (f_name1, f_name2, f_name2.....
d_name
d_id time
1 2018-06-01 (d_name1, dname2, d_name3...
2 2018-06-01 (d_name1, dname2, d_name3...
3 2018-06-01 (d_name1, dname2, d_name3...
我如何获得以下信息d_id 时间 ver d_name f_name其中 ver、d_name 和 f_name 是列表.
How do I get the following d_id time ver d_name f_name where ver, d_name and f_name are lists.
希望这很清楚.
推荐答案
使用单个参数而不是三个
Use a single argument instead of three
{'d_name': lambda x: tuple(x), 'ver': lambda x: tuple(x), 'f_name': lambda x: tuple(x)}
这篇关于带有agg的pandas groupby无法处理多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!