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 ID | Date | Channel |
---|---|---|
1 | 1/1/24 | |
1 | 1/2/24 | Search |
1 | 1/3/24 | Direct |
2 | 1/5/24 | Direct |
2 | 1/6/24 | Paid |
2 | 1/7/24 | |
3 | 1/8/24 | Direct |
3 | 1/9/24 | Direct |
3 | 1/10/24 | Direct |
3 | 1/11/24 | Direct |
And the output I need is this:
Customer ID | First Channel | Last Channel |
---|---|---|
1 | Search | |
2 | Paid | |
3 | Direct | Direct |
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.