简体   繁体   中英

Pandas groupby function with dict.update()

I'm trying to use the Pandas groupby function with a dict.update() function to each element. An example in a data frame (just for illustration):

        A                                            B
0   icon1   {'ap1': {'item' : 1}, 'ap2': {'item' : 2}}

1   icon1                        {'ap3': {'item' : 3}}

What I'm trying to do is set something like

df = df.groupby('A')['B'].apply(', '.join).reset_index()

But instead of using python', '.join , I need to groupby the 'A' column and update each element in the 'B' column. I've tried using the map function, but I was not able to achieve anything useful.

The outcome should be:

        A                                                                 B
0   icon1   {'ap1': {'item' : 1}, 'ap2': {'item' : 2}, 'ap3': {'item' : 3}}

Is that even possible without changing the item type from dict?

Using dict comprehension

df.groupby('A').B.agg(lambda s: {k:v for a in s for k, v in a.items()}).reset_index()

        A                                                                 B
0   icon1   {'ap1': {'item' : 1}, 'ap2': {'item' : 2}, 'ap3': {'item' : 3}}

toolz.dicttoolz.merge

from toolz.dicttoolz import merge

df.groupby('A')['B'].agg(merge).reset_index()

       A                                                  B
0  icon1  {'ap1': {'item': 1}, 'ap2': {'item': 2}, 'ap3'...
1  icon2  {'ap1': {'item': 1}, 'ap2': {'item': 2}, 'ap3'...

Setup

df = pd.DataFrame(dict(
    A=['icon1', 'icon1', 'icon2', 'icon2'],
    B=[{'ap1': {'item': 1}, 'ap2': {'item': 2}}, {'ap3': {'item': 3}}] * 2
))

You can use a helper function:

def func(x):
    dct = {}
    for i in x:
        dct.update(i)
    return dct

df.groupby('A')['B'].agg(func).reset_index()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM