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