Nyxynyx
Nyxynyx

Reputation: 63687

Can Pandas Groupby Aggregate into a List of Objects

While panda's groupby is able to aggregate data with functions like sum and mean, is there a way to aggregate into a list of objects, where the keys of these objects corresponds to the column names these values were aggregated from?

Question:

If the data looked like this

    A    B    C  
    1    10   22
    1    12   20
    1    11   8
    1    10   10
    2    11   13
    2    12   10 
    3    14   0

how can we get Pandas to transform it into this hypothetical output:

    A    D  
    1    [{'B':10, 'C':22}, {'B':12, 'C':20}, {'B':11, 'C':8}, {'B':10, 'C':10}]
    2    [{'B':11, 'C':13}, {'B':12, 'C':10}]
    3    [{'B':14, 'C':0}]

Upvotes: 1

Views: 2917

Answers (2)

cronos
cronos

Reputation: 2448

Use the groups directly. Here is a dict which looks similar to what you want.

g = df.groupby("A")
{a:list(s.drop("A", axis=1).T.to_dict().values()) for a,s in g}

{'1': [{'B': '10', 'C': '22'},
  {'B': '12', 'C': '20'},
  {'B': '11', 'C': '8'},
  {'B': '10', 'C': '10'}],
 '2': [{'B': '11', 'C': '13'}, {'B': '12', 'C': '10'}],
 '3': [{'B': '14', 'C': '0'}]}

Upvotes: 0

chrisb
chrisb

Reputation: 52276

I actually wasn't sure this would work, but seems to.

In [35]: df.groupby('A').apply(lambda x: x.to_dict(orient='records'))
Out[35]: 
A
1    [{u'A': 1, u'C': 22, u'B': 10}, {u'A': 1, u'C'...
2    [{u'A': 2, u'C': 13, u'B': 11}, {u'A': 2, u'C'...
3                       [{u'A': 3, u'C': 0, u'B': 14}]
dtype: object

Depending on what you're trying to accomplish it may be more natural to iterate of the groupby object and convert, like this:

In [36]: for a, df_gb in df.groupby('A'):
    ...:     d = df_gb.to_dict(orient='records')
    ...:     

Upvotes: 1

Related Questions