Home > OS >  Compare a list to a column in a DataFrame. If they match then append to a new column
Compare a list to a column in a DataFrame. If they match then append to a new column

Time:07-29

I have a column in the dataframe that contains a string that ends with a location code. For example, Growers SeGrowersSecret 14AG CHEM

locations = ["AG CHEM", "AG SEED", "BH CHEM", "BH FARM", 'BH GREEN', 'CT CHEM', 'Bighorn Farm', 'Courthouse Farm']
    
   
df["Location Code"] = ""

loc = []
  
for i in df["str"]:
    stlen = len(i)
        
for x in locations:
    loclen = len(x)
    start, stop = stlen - loclen, 50
    if :
        loc.append(x)
  
df["Location Code"]  = loc   

the locations list contains all the possible locations. I want to compare the list to that portion of the string and have a separate column in the dataframe for locations. I tried str.endswith() but it didn't work either.

All help is very appreciated!

CodePudding user response:

Use this code:

def f(x):
    for i in locations:
        if x.find(i)>-1:
            return i
df['location']= df['str'].apply(f)

CodePudding user response:

Given:

                                 col
0  Growers SeGrowersSecret 14AG CHEM

Doing:

locations = ["AG CHEM", "AG SEED", "BH CHEM", "BH FARM", 'BH GREEN', 'CT CHEM', 'Bighorn Farm', 'Courthouse Farm']
regex = '('   '|'.join(locations)   ')'
df['locations'] = df.col.str.extract(regex)
print(df)

Output:

                                 col locations
0  Growers SeGrowersSecret 14AG CHEM   AG CHEM
  • Related