yanachen
yanachen

Reputation: 3753

How to get the value counts of the specified column?

rev_id  worker_id   label
37675   1362    1.0 
37675   2408    0.0 
37675   1493    0.0 
37675   1439    1.0 
37675   170     0.0 
37675   176     0.0 
37675   481     1.0 
37675   487     0.0 
37675   578     0.0

Here is the paragraph of the data frame, I want to group by the rev_id and the value counts of label, label only has two values: 1 & 0. New data frame should have two new columns which is the count of label=1 and label=0 for every rev_id. How to realize it ?

Upvotes: 1

Views: 29

Answers (1)

jezrael
jezrael

Reputation: 862581

I think you need groupby + value_counts and last reshape by unstack:

df = df.groupby('rev_id')['label'].value_counts().unstack()

Or groupby by both columns and use size:

df = df.groupby(['rev_id', 'label']).size().unstack()

print (df)
label   0.0  1.0
rev_id          
37675     6    3

Upvotes: 1

Related Questions