I have two pandas data frame contains millions of rows in python. I want to remove rows from the first data frame that contains words in seconds data frame based on three conditions:
- If the word appears at the beginning of the sentence in a row
- If the word appears at the end of the sentence in a row
- If the word appears in the mid the sentence in a row (exact word, not a subset)
Example:
First Dataframe:
This is the first sentence
Second this is another sentence
This is the third sentence forth
This is fifth sentence
This is fifth_sentence
Second Dataframe:
Second
forth
fifth
Output Expected:
This is the first sentence
This is fifth_sentence
Please note that I have millions of records in both the data frame, how can I process it and export in the most efficient way?
I tried but it takes very much time
import pandas as pd
import re
bad_words_file_data = pd.read_csv("words.txt", sep = ",", header = None)
sentences_file_data = pd.read_csv("setences.txt", sep = ".", header = None)
bad_words_index = []
for i in sentences_file_data.index:
print("Processing Sentence:- ", i, "\n")
single_sentence = sentences_file_data[0][i]
for j in bad_words_file_data.index:
word = bad_words_file_data[0][j]
if single_sentence.endswith(word) or single_sentence.startswith(word) or word in single_sentence.split(" "):
bad_words_index.append(i)
break
sentences_file_data = sentences_file_data.drop(index=bad_words_index)
sentences_file_data.to_csv("filtered.txt",header = None, index = False)
Thanks