2

I'm trying to filter a dataframe with the same condition on multiple columns.

This is possible by doing as follows:

>>> df = pd.DataFrame({
    "A1": [1, 2, 5, 1, 2],
    "B1": [0, 2, 0, 4, 1],
    "A2": [2, 3, 5, 1, 5],
    "B2": [4, 1, 6, 2, 1]
})

>>> df_filtered = df[(df['A1'] > 3) | (df['A2'] > 3)]

>>> df_filtered
   A1  B1  A2  B2
2   5   0   5   6
4   2   1   5   1

However, I'd like to be able to fix the columns programmatically from a predefined variable, instead of manually, like in this example:

cols = ['A1', 'A2']

df_filtered = df[df[cols].any(> 3)]

Except the code just above obviously doesn't work.

2 Answers 2

3

Looks like it is possible by doing:

>>> df_filtered = df[(df[cols] > 3).any(axis=1)]

>>> df_filtered
   A1  B1  A2  B2
2   5   0   5   6
4   2   1   5   1

(This answer was redacted while Wen-Ben was posting his own, but I decided to post it anyway because it offers a slightly different syntax)

Sign up to request clarification or add additional context in comments.

Comments

2

Then we make it work :-)

cols = ['A1', 'A2']
df[df[cols].gt(3).any(axis = 1)]
Out[556]: 
   A1  B1  A2  B2
2   5   0   5   6
4   2   1   5   1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.