简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM