lundi 27 janvier 2020

Add value from column based on other column through loop

I have the following data frame:

ViewKey     Table_Name
car         A
car         B
NaN         C
ball        D
ball        E

and the following sentences:

contains_car = " SELECT \n keys \n FROM *letter here* A \n LEFT JOIN "
contains_ball = " SELECT \n keys \n FROM *letter here* A \n LEFT JOIN "
contains_NaN = " SELECT \n keys \n FROM *letter here* A \n"

I want to "print" for every value of Table_Name the letter of corresponding ViewKey. I was trying to get the following output:

SELECT 
 keys 
 FROM A  
 car 

 SELECT 
 keys 
 FROM B 
 car 

 SELECT 
 keys 
 FROM C
 empty 

 SELECT 
 keys 
 FROM D 
 car 

 SELECT 
 keys 
 FROM E 
 car 

I tried:

for i in df['ViewKey'].fillna("0"):
    if 'car' in i:
        print(contains_car)
    elif 'ball' in i:
        print(contains_ball)
    elif '0' in i:
        print(contains_NaN)

IDK where to put if conditions

Aucun commentaire:

Enregistrer un commentaire