19

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

enter image description here

2 Answers 2

31

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

final_data.loc[selection['RatingCounts']]
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you this has been helpful but I needed to use .ravel() in the end to turn that numpy.ndarray into a one dimensional array.
Yes, it is hard to understand your exact situation without code to reproduce it. In the future you may be able to get more accurate answers by providing a minimal, complete and verifiable example.
This helped me solve a bug. Some newly added code upstream from where the error was being thrown added a new column that happened to have the same name as an existing column in my dataframe. The error was then thrown when I tried to filter on that column because now there were two columns which created a 2 column dataframe rather than a single series. I solved this by renaming the new column to eliminate the duplication.
1

You can possibly get this error if you have multi lvl column indexing.

EX.:

  • How to create the error by renaming columns
existing_df.columns = [['user','movie','ratings']]
  • How to it correctly
existing_df.columns = ['user','movie','ratings']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.