Alejandro Marín
Alejandro Marín

Reputation: 73

Pandas counting how many times a value has been counted in a Group by

I hope my question doesn't sound like a tongue twister.

I have a table with 2 columns, one column (let's call it "customer_ID") is the customer identifier, and the other columns (let's call it "Date") is the date of the purchase of said customer. For example:

Customer_ID Date
807x 2010-2
807x 2010-3
789y 2010-2
789y 2010-4
323z 2010-1
323z 2010-5
323z 2010-6

There are customers who have made one purchase, and customers who have made several.

I can count the number of times each customer has bought with:

df.groupby('customer_id').Count()

However, what I want to count is the number of times each value has been counted. For example, if customer X has bought 2 times, customer Y has bought 2 times, and customer Z has bought 3 times, I want an output like this:

Count Repeated
2 2
3 1

Upvotes: 1

Views: 728

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Try value_counts twice:

df['customer_id'].value_counts().value_counts()

Output:

2    2
3    1
Name: Customer_ID, dtype: int64

Upvotes: 2

Related Questions