python faster way to perform string manipulation without pandas apply?

Viewed 45

I have a list of strings, and I want to check if they are in a certain column. For each string, I will create a new column and output True or False.

keywords = ['1', '2', '3']
for keyword in keywords:
    df[keyword] = df['text'].apply(lambda x: keyword in x)

The problem is, the file is extremely large and each row in the "text" column is thousands of characters long. I know that apply is not good for performance. What is the alternative here?

Note: I know that there might be a solution with .str.contains(), but I want a more general solution that isn't limited to substrings in strings

1 Answers

maybe something like

for keyword in keywords:
    df[keyword]=[keyword in x for x in df['text'].tolist()]
Related