pandas groupby with agg not working on multiple columns
Asked Answered
B

1

0

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)})

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.

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...

How do I get the following d_id time ver d_name f_name where ver, d_name and f_name are lists.

Hope this is clear.

Borne answered 6/9, 2018 at 18:17 Comment(4)
Thanks. Solves it like a charm :). If you could post it, I'll accept as an answer.Borne
One additional thing though, the initial df columns are not part of the resulting dataframe ('d_id', 'time'). Any ideas on how to join the list columns back with the groupby columns?Borne
hm, what do you mean? Can you edit the question with more details?Ducan
Edited my question to add further detailsBorne
D
1

Use a single argument instead of three

{'d_name': lambda x: tuple(x), 'ver': lambda x: tuple(x), 'f_name': lambda x: tuple(x)}
Ducan answered 6/9, 2018 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.