在 pandas eval中调用round(),ceiling(),floor(),min(),max() [英] Calling round(), ceiling(), floor(), min(), max() in pandas eval
问题描述
正如标题所说,在熊猫评估中,有没有一种方法可以支持圆形,天花板,最小,最大,地板功能.
As title says, Is there a way to support round, ceiling, min, max, floor functions in pandas eval.
DataFrame:
DataFrame:
import pandas as pd
import numexpr as ne
op_d = {'ID': [1, 2,3],'V':['F','G','H'],'AAA':[0,1,1],'E':[102014,112019,122017] ,'D':['2019/02/04','2019/02/01','2019/01/01'],'DD':['2019-12-01','2016-05-31','2015-02-15'],'CurrentRate':[7.5,2,2],'NoteRate':[2,3,3],'BBB':[0,00,4],'Q1':[2,8,00],'Q2':[3,5,7],'Q3':[5,6,8]}
df = pd.DataFrame(data=op_d)
abs()和sqrt()函数可与pandas eval一起使用.即
abs() and sqrt() function works with pandas eval. i.e.
df.eval('TT = abs(sqrt(Q1+Q2)-Q2)',inplace=True)
df
有人可以建议如何访问eval中的其余功能吗?我还在eval中尝试了"local_dict",以查看是否可以定义自定义函数并调用它们,但是它没有用.
can anyone suggest how to access rest of the functions in eval? I also tried 'local_dict' in eval to see if I can define custom functions and call them but it didn't work.
注意:
-
这些函数中的
- 算术运算是必需的(即两列的求和,乘法,div).
- 我知道有关使用评估"功能和进行必要测量的问题.
推荐答案
借助"py_expression_eval"库,我能够在用户定义的函数中计算出算术运算.
With the help of 'py_expression_eval' libary, I was able to work out arithmatic operations inside user defined functions.
from py_expression_eval import Parser
parser = Parser()
dct = {'Q1':df['Q1'],'Q2':df['Q2'],'max':max1,'round':getround}
df['check']=parser.parse('round(Q1/Q2)').evaluate(dct)
库源: https://github.com/Axiacore/py-expression-eval
希望这对其他人有帮助.
Hope this helps others.
这篇关于在 pandas eval中调用round(),ceiling(),floor(),min(),max()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!