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.