简体   繁体   中英

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

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

Use this code:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM