我正在尝试复制此处提供的代码: https://github.com/IdoZehori/Credit-Score/blob/master/Credit%20score.ipynb
下面给出的函数无法运行并报错。谁能帮我解决一下
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:我知道之前有人问过这个问题,但我尝试使用这些技术,但错误仍然存在
最佳答案
zip 不能直接使用,你应该以列表的形式给出结果即:
x = pd.DataFrame(list(zip(data, vote)), columns=['annual_income', 'outlier'])
编辑(来自 bayethierno 答案):
从 0.24.0 版本开始,我们不再需要从 zip
生成列表,下面的语句是有效的:
x = pd.DataFrame(zip(data, vote), columns=['annual_income', 'outlier'])
关于Python:数据参数不能是迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45388800/