pandas ValueError:无法使用多维键建立索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48734388/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:09:37  来源:igfitidea点击:

ValueError: Cannot index with multidimensional key

pythonpandas

提问by Furkan K?l??aslan

I am trying to code a simple recommender system using only pandas and I am having trouble with the filtering part.I want to select all the rows where the RatingCounts column is greater than a value I choose.This returns me a dataframe with one column filled with the correct booleans but i cannot index my data with this selection it gives me a value error as mentioned in the title.Here is the screenshot

我正在尝试仅使用 Pandas 编写一个简单的推荐系统,但我在过滤部分遇到了问题。我想选择 RatingCounts 列大于我选择的值的所有行。这会返回一个数据框,其中一列已填充使用正确的布尔值,但我无法使用此选择索引我的数据,它给了我标题中提到的值错误。这是屏幕截图

enter image description here

在此处输入图片说明

回答by unutbu

selectionappears to be a 2D DataFrame with 1 column, RatingCounts. The error occurs when you pass a 2D indexerto DataFrame.loc. You can avoid the error by passing a 1D boolean indexer to DataFrame.loc:

selection似乎是一个带有 1 列的 2D DataFrame,RatingCounts. 出现此错误时,你传递一个二维索引DataFrame.loc。您可以通过将一维布尔索引器传递给 来避免错误DataFrame.loc

final_data.loc[selection['RatingCounts']]