mardi 25 juin 2019

modifying a dataframe by adding additional if statement column

Modifying a data frame by adding an additional column with if statement. I created 5 lists namely: East_Asia, Central_Asia,Central_America,South_America, Europe_East & Europe_West. And I wanted to add a conditional column based on existing column. i.e if japan in Central_East, then the japan row in the adding column should contain Central East, so on.

df['native_region'] =df["native_country"].apply(lambda x: "Asia-East" if x in 'Asia_East' 
                                               "Central-Asia" elif x in "Central_Asia"
                                                "South-America" elif x in "South_America"
                                                "Europe-West" elif x in "Europe_West"
                                                "Europe-East" elif x in "Europe_East"
                                                "United-States" elif x in " 
United-States" 
else "Outlying-US"
                                                 )

File "", line 2 "Central-Asia" elif x in "Central_Asia" ^ SyntaxError: invalid syntax

Aucun commentaire:

Enregistrer un commentaire