I need Python code that takes the text in column x and loops over column y and searches for the substring values x within each value in Y. My example is below. IF possible, I would like it to print the value of the match and the name in a dictionary or someway I convert it to a Pandas dataframe with a value for each column. I'm fairly new at this keep getting errors. My code and error is below.
matches=['cat','bat','fat']
names=['turtle','bigcats','hfat1']
for x in matches:
if name.str.contains(x) == 1:
print(name)
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
解决方案
Since you tagged this question as pandas:
import pandas as pd
import numpy as np
matches=['cat','bat','fat']
names=['turtle','bigcats','hfat1']
df = pd.DataFrame({'Name':names,'Matches':matches})
print(df)
Starting dataframe:
Matches Name
0 cat turtle
1 bat bigcats
2 fat hfat1
Use str access with contains and regex created by join:
df.loc[df.Name.str.contains('|'.join(df.Matches)),'Name'].tolist()
Output:
['bigcats', 'hfat1']