I would like to search within more columns some words selected by input. It could be just one word or more than one (so a list of words). This is how my dataset looks like:
Text1 Text2
Include details about your goal... Include any error messages...
Describe expected and actual results Help you with specific coding, algorithm, or language problems: 4.5 points
Let us say that, for example, I would like to select only elements that contain goal or error, words that I added manually by using input:
I should have:
Text1 Text2
Include details about your goal... Include any error messages...
I could also create a new column that add a boolean value in case there is a match with my search/filter request for one/both columns/terms (True
) or none (False
).
I have tried as follows:
def matcher(x):
for i in list_to_search:
if i.lower() in x.lower():
return i
else:
return np.nan
list_to_search = []
while True:
query = input("Enter your query: ")
list_to_search.append(query)
print(list_to_search)
print(list_to_search)
df['Match'] = df['Text1'].apply(matcher)
However the code above run forever because of while True
condition. From the example above, I would like only to search for goals
or errors
, but I would have also searched for goals
and errors
. And I would also be interested in looking for possible consecutive words (for example 4.5 points
). Finally, I do not know how to check for multiple columns (in the code above I am looking only for matching in column Text1
, not in Text1
and/or Text2
).
I hope you can help me. Thanks
UPDATE: I think I have fixed the issue with the while loop:
list_to_search = []
def matcher(x):
for i in list_to_search:
if i.lower() in x.lower():
return i
else:
return np.nan
while True:
query = input("Enter your query: ")
if query!='-f':
list_to_search.append(query)
print(list_to_search)
if query=='-f':
break
print(list_to_search)
df['Match'] = df['Text1'].apply(matcher)