Home > Software engineering >  Creating a column identifier based on another column
Creating a column identifier based on another column

Time:02-18

I have a df below as

    NAME 
German Rural
 1990 german
Mexican 1998
Mexican City

How can i create a new column based on the values of these columns ( if the column has the term %German% or % german% regardless of capital or lower case or case insensitive?

Desired output

NAME         | Identifier
German Rural    Euro
1990 german     Euro
Mexican 1998 South American 
Mexican City South American

CodePudding user response:

You could do that with something like the following.

conditions = [df["NAME"].str.lower().str.contains("german"), 
               df["NAME"].str.lower().str.contains("mexican")]

values = [ "Euro", 'South American']
    
df["identifiter"] = np.select(conditions, values, default=np.nan)

print(df)
           NAME     identifiter
0  German Rural            Euro
1   1990 german            Euro
2  Mexican 1998  South American
3  Mexican City  South American
  • Related