How to use OR in a regex?

Viewed 14

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?

1 Answers

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

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