TheoretiCAL
TheoretiCAL

Reputation: 20571

Pandas groupby(dictionary) not returning intended result

I'm trying to group the following data:

>>> a=[{'A': 1, 'B': 2, 'C': 3, 'D':4, 'E':5, 'F':6},{'A': 2, 'B': 3, 'C': 4, 'D':5, 'E':6, 'F':7},{'A': 3, 'B': 4, 'C': 5, 'D':6, 'E':7, 'F':8}]
>>> df = pd.DataFrame(a)
>>> df
   A  B  C  D  E  F
0  1  2  3  4  5  6
1  2  3  4  5  6  7
2  3  4  5  6  7  8

With the Following Dictionary:

dict={'A':1,'B':1,'C':1,'D':2,'E':2,'F':2}

such that

df.groupby(dict).groups

Will output

{1:['A','B','C'],2:['D','E','F']}

Upvotes: 1

Views: 291

Answers (1)

TheoretiCAL
TheoretiCAL

Reputation: 20571

Needed to add the axis argument to groupby:

>>> grouped = df.groupby(groupDict,axis=1)
>>> grouped.groups
{1: ['A', 'B', 'C'], 2: ['D', 'E', 'F']}

Upvotes: 2

Related Questions