mercredi 6 septembre 2017

python flag based on conditional if logic using three different columns

I have the following df based on google analytics data:

Customer | transaction_id | medium   | first_transaction_flag
ABC        12345            organic      Y
ABC        23456            email        0    
ABC        34567            organic      0
BCD        45678            organic      0
BCD        56789            referral     0

In the above df, the transaction 12345 has a first_transaction_flag of Y, which means that this was the customer's first transaction ever.

I need to add a second flag labeled first_channel. What it should do is flag for that one customer that channel, where they were acquired as their first_channel = Y for all subsequent transactions. This would be the output:

Customer | transaction_id | medium   | first_transaction_flag | first_channel
ABC        12345            organic      Y                       Y
ABC        23456            email        0                       0
ABC        34567            organic      0                       Y             
BCD        45678            organic      0                       0
BCD        56789            referral     0                       0

Basically, it would be a conditional if statement: if first_transaction_flag = Y, then mark the same combo of customer and medium as Y. I was trying to think if it is possible with a loc or np.where statement, but didn't get far.

Aucun commentaire:

Enregistrer un commentaire