简体   繁体   中英

How to match key words in a pandas series

I want to iterate over all the rows in one of my Dataframe columns named Subject and look for multiple key words from the dictionary named keywords . If a key from the dictionary matches a word in the Dataframe column, I want to add the dictionary value pair for the matched key to a new column in the Dataframe named Category . My code below was my first idea to add values to a list and then add the list as a new column to my Dataframe but obviously the indexes will not match up. Is there a way to append the keyword value directly to the Dataframe whenever a key matches in the subject column?

'''

tickets = pd.read_csv('All VS Tickets with Category.csv',parse_dates=['Creation Date'])
tickets = tickets.fillna('')

keywords = {'BOR':'Broker of Record','New Vendor':'New Vendor Build'}
Category_column = []

def indexmatch(subject):
    for key, value in keywords.items():
        if key in subject:
            print('Match')
            Category_column.append(values)
        else:
            print('No Match')
            Category_column.append('')


tickets['Subject'].apply(indexmatch)
Category_column = pd.Series(Category_column)
tickets.insert(0,column='Category',value=Category_column)

'''

Image of example code

You can extract your joined keywords and then map :

keywords = {'BOR':'Broker of Record','New Vendor':'New Vendor Build'}

df = pd.DataFrame({"Category":["Something BOR","Something New Vendor","Something for nothing"]})

df["new"] = df["Category"].str.extract(f"({'|'.join(keywords)})",expand=False).map(keywords)

print (df)

                Category               new
0          Something BOR  Broker of Record
1   Something New Vendor  New Vendor Build
2  Something for nothing               NaN

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