python - Pandas:如何应用传递的条件运算符来选择 pandas 中的行?

标签 python python-3.x pandas

如何将运算符作为参数传递给函数,并在该函数内部使用该运算符来选择 pandas 行?

# helper.py
def get_milage(operator, condition):
    return df[df.Milage <operator> condition]

# main.py
import operator
get_milage(operator.lt, 7)

最佳答案

我认为需要使用operator() 中,与 operator.lt(column, value) 相同的是:

# helper.py
def get_milage(operator, condition):
    return df[operator(df.Milage, condition)]

关于python - Pandas:如何应用传递的条件运算符来选择 pandas 中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49556145/

相关文章:

python - CPU 为 100% 用于多处理队列以从多个摄像机获取帧

python - 使用Python将包含一些关键字的字符串分成列表

python - Bokeh 'utf8' 编解码器无法解码字节 0xe9 : unexpected end of data

python - Pandas:read.csv() - 只读具有特定列长度的行

python - Selenium Chromedriver - 单击取消 Chrome 身份验证弹出窗口

python - 为什么 ProcessPoolExecutor 忽略 max_workers 参数?

Python绘制时间列表

python - 在 Python 3 中使用 itertools.product 代替双嵌套 for 循环

python - 根据其他数组从 numpy 数组中选择值

Python 的 "StandardScaler"和 "LabelEncoder"以及 "fit"和 "fit_transform"不适用于同时包含 float 和字符串的 CSV