Vivek Kalyanarangan
Vivek Kalyanarangan

Reputation: 9081

Count Distinct Group By Pandas Python

Here's some sample data -

ID  Owner   Activity
1   A   W1
1   A   W2
1   B   W3
1   C   W4
2   C   W5
2   C   W6
2   D   W7
3   A   W8
3   B   W9

I want number of owners per ID. Output will be -

ID  Number of Owners
1   3
2   2
3   2

How can I do this using pandas?

Upvotes: 1

Views: 566

Answers (1)

BrenBarn
BrenBarn

Reputation: 251618

df.groupby('ID').Owner.nunique()

Upvotes: 4

Related Questions