我有一个Excel表,用作逻辑运算符的参考,所以我可以稍后将它们连接起来,将逻辑字符串应用到Pandas Dataframe 。
Dataframe
GOOD BAD UGLY
0 101 60 0
1 22 61 0
2 103 62 NaN
3 104 63 0
我可以从Excel工作表中获取值并将它们添加到列表中。但如何将此逻辑公式解析为df
?
import pandas as pd
import openpyxl
def create_dataframe():
df = pd.DataFrame({'GOOD': [101,22,103,104],
'BAD': [60,61,62,63],
'UGLY': [0,0,'NaN',0],
})
print(df)
read_filter = pd.read_excel('test.xlsx')
print(read_filter)
formulas = []
logicals = ['>','<']
for i, filter_col in enumerate(read_filter['col1']):
if read_filter['Logic'][i] in logicals:
formula = f"df['{filter_col}'][{i}]" + read_filter['Logic'][i] + str(read_filter['value'][i])
formulas.append(formula)
else:
formula = f"{read_filter['Logic'][i]}(df['{filter_col}'])"
formulas.append(formula)
#
print(formulas)
#df['Result'] = df.apply(lambda x: eval(formulas) , axis=1)
return df
公式----
["df['GOOD'][0]>100", "df['BAD'][1]<50", "pd.isna(df['UGLY'])"]
预期结果:
GOOD BAD UGLY Result
0 101 60 0 False
1 22 61 0 False
2 103 62 True
3 104 63 0 False
1条答案
按热度按时间yws3nbqq1#
您可以像这样建立完整条件:
每个表达式都应放在括号中。否则
a > b & c > d
将被解析为a > (b & c) > d
,* 而不是 *(a > b) & (c > d)
。然后
eval
它:然后,您可以使用此结果创建列: