我有一个熊猫数据框,我想过滤掉一些不满足某些条件的记录:
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())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句