I'm trying to create a new column in the below sample data frame that combines the data from the customer_response and the bot_response into a list shown in the combined column of the second table shown below.
One thing to note is that there will always be the same number of delimiters for the customer and bot column for each row.
df
conv_id | customer_response | bot_response |
---|---|---|
1 | Make a payment; Credit Card; $1000; Thanks! | I can help with that. Which account?; What amount?; Payment successful; You're Welcome! |
2 | Replace a card; Ending in 4352; overnight; thanks | I can help replace your card, which account?; How soon do you need it; The replacement card will arrive in 3-5 business days; No problem |
3 | What is my due date?; Am I past due?; thanks | Hello, your due date is the 3rd of each month; No, your account is current; you're welcome! |
Desired output:
conv_id | customer_response | bot_response | combined |
---|---|---|---|
1 | Make a payment; Credit Card; $1000; Thanks! | I can help with that. Which account?; What amount?; Payment successful; You're Welcome! | ["Customer: Make a payment", "Bot: I can help with that. Which account?", "Customer: Credit Card", "Bot: What amount?", "Customer: $1000", "Bot: Payment successful", "Customer: Thanks!", "Bot: You're Welcome!"] |
2 | ... | ... | ... |
3 | ... | ... | ... |
Here is my code so far but for some reason I'm drawing a blank on how to create a column to combine the two as shown in the combined column in the second table above.
df['cust_dil'] = [x for x in df['customer_response'].str.split(';')]
df['bot_dil'] = [x for x in df['bot_response'].str.split(';')]
At a high level, I know I need to loop through the elements in each row after splitting them and add the the respective text "Bot" or "Customer" before each element in the list and then combine the 2 columns in order.
I'm really stumped on this so any help that can be given is greatly appreciated.