Python Dataframe在不同列之间进行代数运算

Python Dataframe perfom algebric operation between different columns

我有一个三列的数据框。我想检查它们是否遵循逻辑顺序。

代码:

df = pd.DataFrame({'low':[10,15,np.nan]','medium:[12,18,29],'high':[16,19,np.nan]})
df = 
    low     medium  high
0   10.0    12      16.0
1   15.0    18      19.0
2   NaN     29      NaN

# check if low<medium<high
df['check'] = (df['low']<df['medium'])&(df['medium']<df['high'])
print("Condition failed: %s"%(df['check'].all()))

当前输出:

df['check']=
True #correct
True # correct
False # wrong output here, it should not consider this

基本上我想避免与 NaN 值进行比较并产生错误输出。我想避开他们。我该怎么做?

你可以mask。此外,您可以使用 between:

而不是链式条件
df['check'] = df['medium'].between(df['low'], df['high'], inclusive='neither').mask(df[['low','high']].isna().any(axis=1))

输出:

    low  medium  high check
0  10.0      12  16.0  True
1  15.0      18  19.0  True
2   NaN      29   NaN   NaN