3

我正在根据阈值从系列中选择数据。

>>> s = pd.Series(np.random.randn(5))
>>> s
0   -0.308855
1   -0.031073
2    0.872700
3   -0.547615
4    0.633501
dtype: float64
>>> cfg = {'threshold' : 0 , 'op' : 'less' }
>>> ops = {'less' : '<', 'more': '>' , 'equal': '==' , 'not equal' : '!='}
>>> ops[cfg['op']]
'<'
>>> s[s < cfg['threshold']]
0   -0.308855
1   -0.031073
3   -0.547615
dtype: float64

我想在最后一行代码中使用 ops[cfg['op']],而不是 '<'。如果需要,我愿意更改 ops dict 的 key 和值(例如 -lt 而不是 <)。如何做到这一点?

4

2 回答 2

4

我很关心@cᴏʟᴅsᴘᴇᴇᴅ 的回答和@Zero 的链接问答...
但是这里有一个替代方案numexpr

import numexpr as ne

s[ne.evaluate('s {} {}'.format(ops[cfg['op']], cfg['threshold']))]

0   -0.308855
1   -0.031073
3   -0.547615
Name: A, dtype: float64

在作为如何将运算符传递给python函数的副本关闭后,我重新打开了这个问题?

问题和答案都很好,我以赞成票表示感谢。

在 a 的上下文中询问pandas.Series可以使用包括numpy和的答案numexpr。而试图用这个答案来回答 dup 目标纯粹是胡说八道。

于 2017-09-26T08:43:17.420 回答
2

定义可以代表您的运算符的方法字典。

import operator    
d = {
         'more'  : operator.gt,
         'less'  : operator.lt,
         'equal' : operator.eq, 
         'not equal' : operator.ne
   }

现在,只需索引到您的字典并应用您的函数参数。

m = d[cfg['op']](s, cfg['threshold'])
m

0    False
1     True
2     True
3    False
4    False
dtype: bool

s[m]

1   -0.262054
2   -1.300810
dtype: float64

这里,

d[cfg['op']](s, cfg['threshold']) 

被翻译成

operator.lt(s, 0)
于 2017-09-26T08:40:11.230 回答