This will improve readability:
As is:
df[(8 < df.foo + df.bar) & (df.foo + df.bar <= 42)].measure.sum()
To be:
df[8 < df.foo + df.bar <= 42].measure.sum()
Comment From: TomAugspurger
This would require changes to the python language to change the order of operations :)
You can use .query
for cases like this:
In [8]: df = pd.DataFrame(np.random.randint(0, 10, size=(10, 2)), columns=['A', 'B'])
In [9]: df.head()
Out[9]:
A B
0 9 0
1 4 6
2 7 9
3 8 3
4 2 7
In [10]: df.query('4 < A < 8')
Out[10]:
A B
2 7 9
8 6 0
In [11]: df.query('4 < A + B < 8')
Out[11]:
A B
5 4 2
8 6 0
Without query, you'll need to add parenthesis:
In [21]: (4 < df.A + df.B) < 8
Out[21]:
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
dtype: bool
Comment From: chris-b1
See also #14138