42piratas
42piratas

Reputation: 595

“groupby” returning the percent of occurrences of each grouped item

This one is similar to my own previous question.

If I perform the following groupby, I get the count() of each factor.

df.groupby("factor")["factor"].count()

Output:

factor
0       2000
1        500
2       1000
3        100

Is there a way to get the % of records belonging to each factor. Expected:

factor
0       55,55
1       13,88
2       27,77
3        2,77

Upvotes: 0

Views: 25

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Normalize parameter might help you:

df['factor'].value_counts(normalize=True)

Upvotes: 1

Related Questions