按可变大熊猫过滤

乔希

我想写一个过滤DataFrame按功能输入的功能。我在这里的第一个尝试是:

def Breakdown(file, key = None, value = None):
    if key is not None:
        if sampreach in ['s', 'S', 'sample', 'Sample']:
            sampreach = sample[sample.key in [value]]
            CountName = 'Total Count'
        elif sampreach in ['r', 'R', 'reachable', 'Reachable']:
            sampreach = reachable[reachable.key in [value]]
            CountName = 'Reachable Count'
    else:
        if sampreach in ['s', 'S', 'sample', 'Sample']:
            sampreach = sample
            CountName = 'Total Count'
        elif sampreach in ['r', 'R', 'reachable', 'Reachable']:
            sampreach = reachable
            CountName = 'Reachable Count'

但是我遇到了以下错误:

AttributeError: 'DataFrame' object has no attribute 'key'

目标是能够设置为key等于列标题value的名称,并设置为要过滤的值的名称,或者更好地设置为要过滤的值的范围。

先谢谢您的帮助!

斯特凡

突出的一件事可能会导致您看到错误:

sampreach = sample[sample.key in [value]]

可能会更好地工作

sampreach = sample[sample[key].isin(value)]

但是,正如评论中所建议的那样,缺少了一些信息(sampreach例如,是什么?)以及示例数据和预期输出,从而可以对发生的情况做出更明智的猜测。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章