Convert pandas.groupby to dict

Question:

Consider, dataframe d:

d = pd.DataFrame({'a': [0, 2, 1, 1, 1, 1, 1],
                  'b': [2, 1, 0, 1, 0, 0, 2],
                  'c': [1, 0, 2, 1, 0, 2, 2]})
>   a   b   c
0   0   2   1
1   2   1   0
2   1   0   2
3   1   1   1
4   1   0   0
5   1   0   2
6   1   2   2

I want to split it by column a into dictionary like that:

{0:    a  b  c
    0  0  2  1,

 1:    a  b  c
    2  1  0  2
    3  1  1  1
    4  1  0  0
    5  1  0  2
    6  1  2  2,

 2:    a  b  c
    1  2  1  0}

The solution I’ve found using pandas.groupby is:

{k: table for k, table in d.groupby("a")}

What are the other solutions?

Asked By: koPytok

||

Answers:

You can use dict with tuple / list applied on your groupby:

res = dict(tuple(d.groupby('a')))

A memory efficient alternative to dict is to create a groupby object and then use get_group:

res = d.groupby('a')
res.get_group(1)  # select dataframe where column 'a' = 1

In cases where the resulting table requires a minor manipulation, like resetting the index, or removing the groupby column, continue to use a dictionary comprehension.

res = {k: v.drop('a', axis=1).reset_index(drop=True) for k, v in d.groupby('a')}
Answered By: jpp
d.groupby('a').apply(lambda dd:dd.to_dict('i')).to_dict()

ouput:

{0: {0: {'a': 0, 'b': 2, 'c': 1}},
 1: {2: {'a': 1, 'b': 0, 'c': 2},
  3: {'a': 1, 'b': 1, 'c': 1},
  4: {'a': 1, 'b': 0, 'c': 0},
  5: {'a': 1, 'b': 0, 'c': 2},
  6: {'a': 1, 'b': 2, 'c': 2}},
 2: {1: {'a': 2, 'b': 1, 'c': 0}}}
Answered By: G.G