Created
June 6, 2019 17:39
-
-
Save ravishchawla/0db9c9c227cca99aa878c502022eeb52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| transcript_portfolio = pd.merge(transcript, portfolio, left_on='offer_id', right_on='id', how='left') | |
| transcript_by_group = transcript_portfolio.groupby(['person', 'offer_id']) | |
| completion_details = []; | |
| ''' | |
| Go through each group in the transaction grouping. Because iterating can be slow, | |
| we will use vectorized operations inside the main loop. | |
| ''' | |
| for i, g in transcript_by_group: | |
| record = {}; | |
| # Record status based on weather the offer has been viewed | |
| if g[['event_offer_received', 'event_offer_viewed', 'event_offer_completed']].sum(axis=0).sum() == 3: | |
| record['status'] = 'viewed_and_completed'; | |
| elif (g[['event_offer_received', 'event_offer_completed']].sum(axis=0).sum() == 2) and 'status' not in record: | |
| record['status'] = 'not_viewed_and_completed'; | |
| else: | |
| record['status'] = 'no_offer'; | |
| # Get required details | |
| person_id, offer_id = g['person'].iloc[0], g['offer_id'].iloc[0]; | |
| first_purchase, last_purchase = g['time'].min(), g['time'].max(); | |
| # Get all transactions corresponding to the person | |
| try: | |
| person_transactions = transactions_only.get_group(person_id); | |
| except KeyError: | |
| offer_transactions = []; | |
| # Filter out transactions that are within the time window of the offer | |
| offer_transactions = person_transactions[(person_transactions.time > first_purchase) \ | |
| & (person_transactions.time < last_purchase)]; | |
| # Store these details into the dictionary | |
| record['num_transactions'] = len(offer_transactions); | |
| record['person_id'], record['offer_id'] = person_id, offer_id; | |
| record['first_purchase'], record['last_purchase'] = first_purchase, last_purchase; | |
| #print(person_id, offer_id, num_transactions) | |
| completion_details.append(record); | |
| completion_details_df = pd.DataFrame(completion_details); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment