Skip to content Skip to sidebar Skip to footer

Filtering Out Rows That Have A String Field Contained In One Of The Rows Of Another Column Of Strings

With two Pandas datasets csv1 = pandas.read_csv('test1') csv2 = pandas.read_csv('test2') how to display all rows of csv1 that have a str1 field that is not a substring of any of t

Solution 1:

The challenge with this problem is to not only detect whether there exists a match, but to figure out what matches what, and filter accordingly. One option using str.contains in a comprehension:

csv1 = csv1.iloc[[~csv2.str2.str.contains(x).any() for x in csv1.str1]]
print(csv1)
   id str1
1   2  def
3   4  xyz

Post a Comment for "Filtering Out Rows That Have A String Field Contained In One Of The Rows Of Another Column Of Strings"