Quantcast
Viewing all articles
Browse latest Browse all 14301

Loop through each customer records to get the first/last channel they came from

I have customer visit records with the channel they came from. I want to have one record per customer where I have the first channel they came from and the last channel they came from.

Another logic I need to add is that if the first channel is "Direct", then do not take it and look at the next record. If that next record is also "Direct", then do not take it either and look at the next record. If you finished all records for the customer and they were all "Direct", then you can make first channel "Direct".I need the same logic for the last channel. Get the channel, if it is "Direct", then go to the previous one. If it is "Direct", then go through the previous record, until you finish all records.

The input data looks like this:

Customer IDDateChannel
11/1/24Email
11/2/24Search
11/3/24Direct
21/5/24Direct
21/6/24Paid
21/7/24Email
31/8/24Direct
31/9/24Direct
31/10/24Direct
31/11/24Direct

And the output I need is this:

Customer IDFirst ChannelLast Channel
1EmailSearch
2PaidEmail
3DirectDirect

How can I do that in my DataFrame?

Probably I need to create two For loops, one to loop through all records, and inside it another loop to go through each customer record. It assigns first channel, then it checks if it is "Direct", then it goes to the next record, until the end of the loop.


Viewing all articles
Browse latest Browse all 14301

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>