count/rank a list in python base on occurance

Viewed 527

I'd like to write this script in python. I have a dataset of a bunch of credit card transactions. That include the date, account number, the amount (either a purchase or a credit/refund) Data will look like this

Acct Number --- Amount
4445 --- $20
4445 --- $30
4445 --- $30
7555 --- $50
7555 --- $50
7555 --- $60

First, I'd like combine the account number with the amount,something like this

4445 | 20
4445 | 30
4445 | 30

7555 | 50
7555 | 50
7555 | 60

Then, I will count each tuples within the whole list based on the occurance of them. For example:

4445 | 20 | 1
4445 | 30 | 1
4445 | 30 | 2

7555 | 50 | 1
7555 | 50 | 2
7555 | 60 | 1

As you can see, the tuple ( 4445 | 20 ) appears only once, I want to count as 1 The tuple ( 4445 | 30 ) appears twice, so I want to count the first tuple as 1 and the second as 2.

Same, tuple (7555 | 50) appears twice, the first appearance will count as 1, next as 2, and the (7555 | 60) only shows once, so I count as 1.

I'm trying several ways but it didn't give me what I want. Really appreciate your help.

Thanks.

3 Answers
Related