【发布时间】:2017-09-29 20:19:46
【问题描述】:
我正在使用 Pandas 和 Flask 开展一个项目,但遇到了以下奇怪的错误:IndexError: single positional indexer is out-of-bounds。然而,错误本身并不奇怪,奇怪的是它是如何发生的。我有一个烧瓶应用程序,其中一个页面是进行 ajax 调用的表单。用户输入书名并返回该书的平均评分。如果我输入“它”,那么评分就会返回。一切正常。但是,如果我输入火车上的女孩,我会收到以下错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/mikecuddy/Desktop/Coding/Python/book_ratings/app.py", line 75, in book_look_up
rating = book.book_rating(title)
File "/Users/mikecuddy/Desktop/Coding/Python/book_ratings/books.py", line 17, in book_rating
rating = info.iloc[0][12]
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py", line 1296, in __getitem__
return self._getitem_axis(key, axis=0)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py", line 1612, in _getitem_axis
self._is_valid_integer(key, axis)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py", line 1526, in _is_valid_integer
raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds
但是,如果我在我的 pandas 代码中输入相同的标题,我会得到评分!我知道我的 Pandas 代码运行良好。我还要说,任何书名只有一个单词,例如它,都可以正常工作。只有标题中有多个单词才会出现我的错误。我已经将问题追溯到此,但无法进一步解决。现在我的代码:
HTML 代码
<form class='title_lookUp_div' id='book_look_up_form'>
<label>Book Title: </label><input id='title' name='title' type='text'>
<p>Result: <span id='result'>?</span></p>
<button>Submit</button>
</form>
JS/AJAX 调用:
$(document).ready(function(){
$('#book_look_up_form').bind('submit', function(event){
event.preventDefault();
$.ajax({
data: {
title: $('#title').val()
},
type: 'POST',
url: '/book_look_up',
success: function(data){
$('#result').text(data.result).show();
}
});
});
});
熊猫代码:
def book_rating(self, title):
#Gettting the row of data for the title that the user entered.
info = self.__data[self.__data.original_title == title]
rating = info.iloc[0][12]
return rating
app.py 代码:
@app.route('/book_look_up',methods=['POST'])
def book_look_up():
#Recieving the data from the ajax call
title = request.form['title']
#The data in the CSV file has the first letter of each word capitalized
title = title.title()
#Creating the object that will deal with the data from CSV file.
book = Books()
rating = book.book_rating(title)
if title:
return jsonify(result = rating)
return jsonify({'error' : 'Missing Data'})
就像我说的,Pandas 代码运行良好,app.py 和 ajax 调用运行良好。只是当数据中有多个单词时,一切都崩溃了,我得到了错误消息。任何帮助都会很棒!谢谢!
【问题讨论】:
-
当熊猫代码崩溃时,
info的值是多少? -
抱歉耽搁了,info的值为:Empty DataFrame。但是,我很确定错误是我的——输入的内容必须与 CSV 文件中的内容完全匹配。所以我有 .title() 方法将火车上的女孩变成了火车上的女孩,而应该是前者!必须想出一个更好的方法来匹配!
-
因此,我意识到我的错误在于让用户输入与 csv 文件中的内容相匹配。
标签: javascript ajax pandas csv flask