Filter dataframe in python using pandas based on partial string match

My Input Dataframe is:

list_of_dicts1 = {"Filter":["abc",'def']}

test1 = pd.DataFrame(list_of_dicts1)

list_of_dicts2 = {"C":["a",'z']}

test2 = pd.DataFrame(list_of_dicts2)

Output Desired is

list_of_dicts3 = {"Filter":['abc']}

test3 = pd.DataFrame(list_of_dicts3)

How can I filter dataframe test1 based on “C” column of test2 dataframe using pandas

Answer

Use Series.str.contains with join by | for regex or:

df = test1[test1['Filter'].str.contains('|'.join(test2['C']))]
print (df)
  Filter
0    abc