简体   繁体   English

根据部分字符串匹配,使用 pandas 在 python 中过滤 dataframe

[英]Filter dataframe in python using pandas based on partial string match

My Input Dataframe is:我的输入 Dataframe 是:

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 Output 所需的是

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如何使用 pandas 基于 test2 dataframe 的“C”列过滤 dataframe test1

Use Series.str.contains with join by |使用Series.str.containsjoin by | for regex or :对于正则表达式or

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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