Viktor Demin
Viktor Demin

Reputation: 364

Repurchase count with pandas

I have such table:

Date       Buyer_id
11.11.2016  1
11.11.2016  2
11.11.2016  2
13.12.2016  1
13.12.2016  3
13.12.2016  4
14.12.2016  3
14.12.2016  1

I need to calculate in pandas Repurchase number So i need number of people who visited shop in different days (it's important). So buyers #1,3 are good, buyer #2,4 are bad.

result should be: good_buyers = 2 bad_buers = 2

Upvotes: 0

Views: 153

Answers (1)

Alex Fung
Alex Fung

Reputation: 2006

This would yield the result you want:

s = (df.groupby('Buyer_id')['Date']
         .apply(lambda x: 'Good' if len(x.unique()) > 1 else 'Bad')
         .value_counts())
print(s)

Output:

Good    2
Bad     2
Name: Date, dtype: int64

If you need a dictionary of the result:

d = s.to_dict()
print(d)

Output:

{'Bad': 2, 'Good': 2}

Upvotes: 1

Related Questions