Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a column identifier based on another column

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

You could do that with something like the following.

df["Identifier"] = (df["NAME"].str.lower().replace(
                     to_replace = ['german', 'mexican'], 
                     value = ['Euro', 'South American']
                   ))

print(df)
      NAME      Identifier
0   German            Euro
1   german            Euro
2  Mexican  South American
3  mexican  South American


Or, the following will match cells that contain additional characters.

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      Identifier
0   German            Euro
1   german            Euro
2  Mexican  South American
3  mexican  South American
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading