我正在根据阈值从系列中选择数据。
>>> 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 而不是 <)。如何做到这一点?