Combining For Loop Iterations Into A Single Line And No Match Handling
Probably a very basic question but hoping someone can help out. I have the following: query = ['whole regular milk', 'gatorade is better', 'whole almond chocolate milk', 'chocolat
Solution 1:
If your input is already a datarframe, you can do the whole thing in the dataframe level:
import re
query = ['whole regular milk', 'gatorade is better',
'whole almond chocolate milk', 'chocolate milk', 'wholes']
types = [{'type': t, 'regex': re.compile(r'\b{}\b'.format(t))}
for t in ['whole', 'regular', 'chocolate']]
df = pd.DataFrame({'Query': query})
def check(q):
return ' | '.join(type_info['type'] for type_info in types
if type_info['regex'].findall(q))
df['Type'] = df['Query'].apply(check)
print(df)
# Query Type
# 0 whole regular milk whole | regular
# 1 gatorade is better
# 2 whole almond chocolate milk whole | chocolate
# 3 chocolate milk chocolate
# 4 wholes
Post a Comment for "Combining For Loop Iterations Into A Single Line And No Match Handling"