Home > Back-end >  How to use OR in a regex?
How to use OR in a regex?

Time:09-23

I am trying to select only columns from a dataframe that start with a p or that contain an s. I am using the following:

df2 = (df.filter(regex ='(^p)' or '(s)'))
df2

But that only selects columns that start with a p. It ignores the second part and doesn't select columns that have an s in the column name. Anyone knows how can I filter so that both conditions are true and my algorithm select both, columns starting with P and also columns that contain an s?

CodePudding user response:

Use the pipe character | which is equivalent to OR in regex.

df2 = (df.filter(regex ='^p|s'))
  • Related