jeudi 7 septembre 2017

python pandas: lookup value in one column conditioned on other column

I have the following df:

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                      

I need to add a column with the actual first medium. This is the end product I am looking for:

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

Basically, whatever is the value in the "medium" column for a transaction that has "first_transaction_flag" = Y, copy that value for all transactions for that client in the "first_medium" column.

I think I'm really close to solving it with the help of one of @piSquared previous answer's.

df['first_medium'] = df.lookup(df.Customer, df.medium)

But not sure how to combine this with a np.where statement for first_transaction_flag =='Y'

So sorry if this was asked before, I couldn't find it

Aucun commentaire:

Enregistrer un commentaire