I'm trying to return all rows from my Dataframe where two conditions are met.
The first condition works just fine. The second condition (where I try to use nlargest(10) to return the rows based on top 10 scores) gives me the following error:
File "/Users/[extracted]/Desktop/imdbnew.py", line 21, in <module>
comedy_high = IMDB[IMDB['Score'].nlargest(10)]
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/frame.py", line 2806, in __getitem__
indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 1552, in _get_listlike_indexer
self._validate_read_indexer(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 1640, in _validate_read_indexer
raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Float64Index([9.6, 9.4, 9.4, 9.4, 9.4, 9.3, 9.2, 9.1, 9.1, 9.0], dtype='float64')] are in the [columns]"
The code that produces this error is as follows:
import pandas
from pandas import DataFrame
import numpy
# Import IMDB data
data = pandas.read_csv('movies.csv')
col = data[['Title', 'Year', 'Score', 'Genre', 'Director',
'Runtime', 'Revenue']]
IMDB = pandas.DataFrame(data, columns = ['Title', 'Year', 'Score', 'Genre',
'Director', 'Runtime', 'Revenue'])
comedy_high = IMDB[IMDB['Score'].nlargest(10)]
#comedy_df = IMDB[(IMDB['Genre'].str.contains("Comedy"))]
print(comedy_high)
If, however, I try to just print out the top 10 scores, and not return the rows corresponding to it from the Dataframe, I do get a result:
comedy_high = IMDB['Score'].nlargest(10)
Where the result is:
9603 9.6
1645 9.4
3914 9.4
5482 9.4
5979 9.4
0 9.3
9 9.2
5428 9.1
6891 9.1
1 9.0
Name: Score, dtype: float64
This has been truly frustrating me, can someone please help out? I'm a novice programmer; I've been reading other similar questions, and testing out answers provided, but am unable to reach a solution. I would really appreciate the help!