How to invert a regular expression in pandas filter function

Matching all lines with no two leading underscores:

^(?!__)

^ matches the beginning of the line (?!__)makes sure the line (what follows the preceding ^ match) does not begin with two underscores

Edit: dropped the .*?$ since it's not necessary to filter the lines.


You have two possibilities here:

(?!^__) # a negative lookahead
        # making sure that there are no underscores right at the beginning of the line

Or:

^\w+  # match word characters, aka a-z, A-Z, 0-9 at least once

I had the same problem but I wanted to filter the columns. Thus I am using axis=1 but concept should be similar.

df.drop(df.filter(regex='my_expression').columns,axis=1)