1
我想编写一个函数,通过函数输入来过滤DataFrame
。在这里,我第一次尝试是:由变量pandas过滤
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
作为名称要筛选的值,或更好的筛选值的范围。
在此先感谢您的帮助!脱颖而出,并可能导致你看到的错误
您能否提供一些样品数据和预期结果? – Alexander