Sreeram TP
Sreeram TP

Reputation: 11907

Find the count of elements in a group Pandas

I have a sample data set like this,

toy_df = pd.DataFrame()

toy_df['A'] = ['ABC', 'ABC', 'XYZ', 'ABC', 'XYZ']
toy_df['B'] = ['AX', 'BX', 'AX', 'AX', 'AX']

I want to group this dataset by columns 'A' and then find the number of occurrences of each value in 'B' as a dict.

While grouping and finding unique elements I get like this,

toy_df.groupby(by=['A'])['B'].unique()

A
ABC    [AX, BX]
XYZ        [AX]
Name: B, dtype: object

I want to get something like shown below,

A
ABC    {'AX' : 2, 'BX' : 1}
XYZ    {'AX' : 2}
Name: B, dtype: object

How can I do this in pandas.?

Upvotes: 0

Views: 45

Answers (1)

jezrael
jezrael

Reputation: 862406

Use dict.fromkeys:

a  = toy_df.groupby(by=['A'])['B'].unique().apply(lambda x: dict.fromkeys(x, 1))
print (a)
A
ABC    {'AX': 1, 'BX': 1}
XYZ             {'AX': 1}
Name: B, dtype: object

EDIT: For count values to dictionary use Counter in dictionary comprehension:

from collections import Counter

a  = pd.Series({i: Counter(g) for i, g in toy_df.groupby(by=['A'])['B']})
print (a)
ABC    {'AX': 2, 'BX': 1}
XYZ             {'AX': 2}
dtype: object

Upvotes: 3

Related Questions