2017-07-29 46 views
0

我試圖複製是這裏提供的代碼: https://github.com/IdoZehori/Credit-Score/blob/master/Credit%20score.ipynb的Python:數據參數不能是一個迭代

下面給出的函數無法運行,並給出錯誤。有人可以幫助我解決它

def replaceOutlier(data, method = outlierVote, replace='median'): 
'''replace: median (auto) 
      'minUpper' which is the upper bound of the outlier detection''' 
vote = outlierVote(data) 
x = pd.DataFrame(zip(data, vote), columns=['annual_income', 'outlier']) 
if replace == 'median': 
    replace = x.debt.median() 
elif replace == 'minUpper': 
    replace = min([val for (val, vote) in list(zip(data, vote)) if vote == True]) 
    if replace < data.mean(): 
     return 'There are outliers lower than the sample mean' 
debtNew = [] 
for i in range(x.shape[0]): 
    if x.iloc[i][1] == True: 
     debtNew.append(replace) 
    else: 
     debtNew.append(x.iloc[i][0]) 

return debtNew 

函數調用:

incomeNew = replaceOutlier(df.annual_income, replace='minUpper') 

Error: x = pd.DataFrame(zip(data, vote), columns=['annual_income', 'outlier']) TypeError: data argument can't be an iterator

PS:我知道這已經被問過,但我想使用的技術,不過仍然存在錯誤

回答

1

zip不能直接使用,您應該給出結果列表即:

x = pd.DataFrame(list(zip(data, vote)), columns=['annual_income', 'outlier']) 
+0

我試過你的建議,但現在出現了一個新的錯誤:'TypeError:list()最多隻需要1個參數(給出2個)' – user4943236

+0

@ user4943236這個列表只需要我的例子中的zip? – PRMoureu

相關問題