Reputation: 70
i have a dataframe like this :
customer_id | trx_date | trx_amount | last_trx |
---|---|---|---|
CS5295 | 2013-02-11 | 35 | 2014-11-25 |
CS5295 | 2014-11-25 | 54 | 2014-11-25 |
CS5295 | 2013-05-22 | 36 | 2014-11-25 |
CS5295 | 2012-12-24 | 36 | 2014-11-25 |
CS5295 | 2013-03-22 | 43 | 2014-11-25 |
CS5295 | 2013-02-11 | 25 | 2014-11-25 |
how can i count the trx_amount, from last month in last_trx, and customer_id as a parameter?
Expected Output :
customer_id | trx_times |
---|---|
CS5295 | 1 |
from the last month of the date in last_trx, 2014-11-25
counting how many times did customer_id make a trx from 2014-10-25 to 2014-11-25
Upvotes: 2
Views: 500
Reputation: 81
For simplicity I will call the DataFrame transact_df
and assume it is a pandas dataframe. Additionally, the "last_trx"
column is assumed be already be in the dataframe.
Convert the columns to DateTime if not already done: Convert Pandas Column to DateTime
Identify all rows that qualify: How would I compute exactly 30 days into the past with Python (down to the minute)?
valid_transacts_df = transact_df.loc[(transact_df['trans_date']
> transact_df['last_trx'] - datetime.timedelta(30))]
# Number of transactions in the last 30 days
trx_times = len(valid_transacts_df)
# Sum of transactions in the last 30 days
total_amount = sum(valid_transacts_df['trx_amount'])
If necessary, you can combine steps 2 & 3 to shorten your code.
Upvotes: 2