Pandas 按位过滤给出带括号和不带括号的不同结果

Pandas bitwise filtering gives different results with and without parenthesis

我有一个 pandas 数据框,我想过滤掉一些不满足特定条件的记录:

cond1=df['ap_lo'] <= df['ap_hi']
cond2=df['height'] >= df['height'].quantile(q=0.025)
cond3=df['height'] <= df['height'].quantile(q=.957)
cond4=df['weight'] >= df['weight'].quantile(q=0.025)
cond5=df['weight'] <= df['weight'].quantile(q=0.975)

这给出了原始数据的以下百分比:

filtered_df=df[cond1 & cond2 & cond3 & cond4 & cond5]
print(filtered_df.shape[0] / df.shape[0]) 
>>>0.8865

令我惊讶的是,当我尝试相同但在条件之间使用括号而不是预定义条件变量时,我得到了不同的结果:

filtered_df = df[(df['ap_lo'] <= df['ap_hi']) & 
                 (df['height'] >= df['height'].quantile(0.025)) &
                 (df['height'] <= df['height'].quantile(0.975)) &
                 (df['weight'] >= df['weight'].quantile(0.025)) & 
                 (df['weight'] <= df['weight'].quantile(0.975))]
print(filtered_df.shape[0] / df.shape[0])
>>> 0.9037

这是怎么回事?我认为这与执行这些操作的顺序无关,因为据我所知,AND 是关联的和可交换的...

@luuk 的评论是正确的;问题是你在第三个条件的第二个定义中调换了数字。

import pandas as pd
import numpy as np

df = pd.DataFrame(data=np.random.uniform(size=(250,4)), columns=['ap_hi', 'ap_lo', 'height', 'weight'])

a = (df['height'] <= df['height'].quantile(q=0.975))
b = (df['height'] <= df['height'].quantile(q=0.957))
print((a == b).all())