I have two Dataframes extracted from a large hotel database:
- A customer shopping history dataframe (df_hist)
customer_id item date
1234 milk 2012-04-20
1234 sugar 2012-05-01
5678 salt 2017-07-15
5678 water 2017-08-10
- A customer visit history dataframe (df_visit)
customer_id start end visit
1234 2012-04-06 2012-04-25 1
5678 2017-07-10 2017-07-20 5
5678 2017-08-05 2017-08-11 6
I'm trying to find out the visit number for each item in the purchase history
- Result(df_result):
customer_id item date visit
1234 milk 2012-04-20 1
1234 sugar 2012-05-01 null
5678 salt 2017-07-15 5
5678 water 2017-08-10 6
I tried using multiple for loops but it's not scalable given that df_visit has close to 6 million rows corresponding to around 15,000 unique customers. What would be a more efficient approach to solve this issue?