Home > Enterprise >  Create new column based on value of another column
Create new column based on value of another column

Time:02-18

I have a solution below to give me a new column as a universal identifier, but what if there is additional data in the NAME column, how can I tweak the below to account for a wildcard like search term?

I want to basically have so if German/german or Mexican/mexican is in that row value then to give me Euro or South American value in new col

    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




Desired output
            NAME             Identifier
    0    1990 German           Euro
    1   german 1998            Euro
    2  country Mexican     South American
    3  mexican city 2006   South American

CodePudding user response:

Based on an answer in this post:

r = '(german|mexican)'

c = dict(german='Euro', mexican='South American')

df['Identifier'] = df['NAME'].str.lower().str.extract(r, expand=False).map(m)

Another approach would be using np.where with those two conditions, but probably there is a more ellegant solution.

CodePudding user response:

below code will work. i tried it using apply function but somehow can't able to get it. probably in sometime. meanwhile workable code below

df3['identifier']=''
js_ref=[{'german':'Euro'},{'mexican':'South American'}]
for i in range(len(df3)):
    for l in js_repl:
        for k,v in l.items():
            if k.lower() in df3.a[i].lower():
                df3.identifier[i]=v
                break
  • Related