Related: Selecting with complex criteria from pandas.DataFrame
I have some DataFrame:
df = pd.DataFrame({'name': ['apple1', 'apple2', 'apple3', 'apple4', 'orange1', 'orange2', 'orange3', 'orange4'],
'A': [0, 0, 0, 0, 0, 0 ,0, 0],
'B': [0.10, -0.15, 0.25, -0.55, 0.50, -0.51, 0.70, 0],
'C': [0, 0, 0.25, -0.55, 0.50, -0.51, 0.70, 0.90],
'D': [0.10, -0.15, 0.25, 0, 0.50, -0.51, 0.70, 0.90]})
df
name A B C D
0 apple1 0 0.10 0.00 0.10
1 apple2 0 -0.15 0.00 -0.15
2 apple3 0 0.25 0.25 0.25
3 apple4 0 -0.55 -0.55 0.00
4 orange1 0 0.50 0.50 0.50
5 orange2 0 -0.51 -0.51 -0.51
6 orange3 0 0.70 0.70 0.70
7 orange4 0 0.00 0.90 0.90
Now let's say I want to select all the rows with values less than 0.25 in A
, B
, C
, and D
:
df[(df['A'] < 0.25) &
(df['B'] < 0.25) &
(df['C'] < 0.25) &
(df['D'] < 0.25)]
name A B C D
0 apple1 0 0.10 0.00 0.10
1 apple2 0 -0.15 0.00 -0.15
3 apple4 0 -0.55 -0.55 0.00
5 orange2 0 -0.51 -0.51 -0.51
Great, but can I achieve the same thing using a list of columns as input?
Imagine that I wanted to filter on 100 columns instead of 4.