Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. transcript_portfolio = pd.merge(transcript, portfolio, left_on='offer_id', right_on='id', how='left')
  2. transcript_by_group = transcript_portfolio.groupby(['person', 'offer_id'])
  3.  
  4. completion_details = [];
  5.  
  6. '''
  7. Go through each group in the transaction grouping. Because iterating can be slow,
  8. we will use vectorized operations inside the main loop.
  9. '''
  10. for i, g in transcript_by_group:
  11.  
  12. record = {};
  13.  
  14. # Record status based on weather the offer has been viewed
  15. if g[['event_offer_received', 'event_offer_viewed', 'event_offer_completed']].sum(axis=0).sum() == 3:
  16. record['status'] = 'viewed_and_completed';
  17. elif (g[['event_offer_received', 'event_offer_completed']].sum(axis=0).sum() == 2) and 'status' not in record:
  18. record['status'] = 'not_viewed_and_completed';
  19. else:
  20. record['status'] = 'no_offer';
  21.  
  22. # Get required details
  23. person_id, offer_id = g['person'].iloc[0], g['offer_id'].iloc[0];
  24. first_purchase, last_purchase = g['time'].min(), g['time'].max();
  25.  
  26. # Get all transactions corresponding to the person
  27. try:
  28. person_transactions = transactions_only.get_group(person_id);
  29. except KeyError:
  30. offer_transactions = [];
  31.  
  32. # Filter out transactions that are within the time window of the offer
  33. offer_transactions = person_transactions[(person_transactions.time > first_purchase) \
  34. & (person_transactions.time < last_purchase)];
  35.  
  36. # Store these details into the dictionary
  37. record['num_transactions'] = len(offer_transactions);
  38. record['person_id'], record['offer_id'] = person_id, offer_id;
  39. record['first_purchase'], record['last_purchase'] = first_purchase, last_purchase;
  40.  
  41. #print(person_id, offer_id, num_transactions)
  42. completion_details.append(record);
  43.  
  44. completion_details_df = pd.DataFrame(completion_details);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement