unbik
unbik

Reputation: 186

How to aggregate DataFrame to count valus per ID in also with other columns in output Python Pandas?

I have DataFrame in Python Pandas like below:

ID  | COL1 | ... | COL_n
----|------|-----|-------
123 | P    | ... | ...
123 | P    | ... | ...
123 | M    | ... | ...
444 | BM   | ... | ...
567 | BM   | ... | ...
29  | P    | ... | ...

And I need to aggregate above columns so as to have output like below:

How can I do that in Python Pandas ?

Upvotes: 2

Views: 32

Answers (1)

BENY
BENY

Reputation: 323306

You could do two steps with pd.crosstab

s = (pd.crosstab(df['ID'],df['COL1'])[['M','P']].
        assign(MP=lambda x : x.sum(1)).
        reindex(df['ID']).
        add_prefix('X_'))
s.index = df.index
df = df.join(s)
Out[178]: 
    ID COL1  X_M  X_P  X_MP
0  123    P    1    2     3
1  123    P    1    2     3
2  123    M    1    2     3
3  444   BM    0    0     0
4  567   BM    0    0     0
5   29    P    0    1     1

Upvotes: 1

Related Questions