'How to assign a unique code based on keywords in Python?

I am trying to assign a predetermined code based on keywords in a string.
For example: ‘Repaired computer per tech manual’ ‘Cleaned connector and now it works’

If ‘repair’ then input in new column ‘R1’. If ‘cleaned’ input ‘C1’

Ultimately I want to assign a code based on multiple keywords. Such as ‘repaired computer’ = R1, ‘repaired wire’ = R2, etc.

I tried .find then .replace but it doesn’t seem to be the right answer. Eventually I want to put this to work on data frames with 150k+ rows.

I’m using SpaCy for preprocessing and could find anything that would meet my above needs. I am new to Python/NLP/coding so much could be rookie mistakes. Figured I’d try the community for help.

Thanks in advance!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source