Rati Goyal
Rati Goyal

Reputation: 69

how to do count of particular value of given column corresponding to other column

input

output

To count the particular value of given column

Upvotes: 0

Views: 30

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34046

Use pd.crosstab with df.sum:

In [236]: output = pd.crosstab(df['Rel_ID'], df['Values'])
In [238]: output['total'] = output.sum(axis=1)

In [239]: output
Out[239]: 
Values  400.0  500.0  1700.0  6300.0  total
Rel_ID                                     
TESTA       1      1       1       1      4
TESTB       1      0       1       1      3
TESTC       0      1       1       0      2
TESTD       1      0       1       1      3
TESTE       1      1       0       0      2

Upvotes: 1

Related Questions