pandas Python:数据参数不能是迭代器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45388800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: data argument can't be an iterator
提问by user4943236
I'm trying to replicate the code that is provided here: https://github.com/IdoZehori/Credit-Score/blob/master/Credit%20score.ipynb
我正在尝试复制此处提供的代码:https: //github.com/IdoZehori/Credit-Score/blob/master/Credit%20score.ipynb
The function given below fails to run and give error. Can someone help me resolving it
下面给出的函数无法运行并给出错误。有人能帮我解决吗
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
Function Call:
函数调用:
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
错误:x = pd.DataFrame(zip(data, vote), columns=['annual_income', 'outlier']) TypeError:数据参数不能是迭代器
PS: I understand this has been asked before, but I tried using the techniques however the error still remains
PS:我知道以前有人问过这个问题,但是我尝试使用这些技术但是错误仍然存在
回答by PRMoureu
zip cannot be used directly, you should give the result as a list i.e.:
zip 不能直接使用,您应该将结果作为列表给出,即:
x = pd.DataFrame(list(zip(data, vote)), columns=['annual_income', 'outlier'])
Edit(from bayethiernoanswer) :
Since the release 0.24.0, we don't need to generate the list from the zip
anymore, the following statement is valid :
编辑(来自bayethierno 的回答):从0.24.0
版开始,我们不再需要从 生成列表zip
,以下语句是有效的:
x = pd.DataFrame(zip(data, vote), columns=['annual_income', 'outlier'])
回答by Rajendra Prasad Taidala
write like this
像这样写
coef = DataFrame(list(zip(x.columns,np.transpose(log_model.coef_))))
回答by bayethierno
This actually works in pandas version 0.24.2 without having to use list around zip
这实际上适用于 Pandas 0.24.2 版,而无需在 zip 周围使用列表