Search code examples
pythonpandasdataframepandas-groupbypartial

Python: combining str.contains and df.groupby successfully in pandas


I am quite a new programmer and I am really struggling with a project that I am working on. I have a movie data list where I'm trying to show the top 10 scores of a movie under a given movie genre.

Here is what I have so far:

import pandas
from pandas import DataFrame

data = pandas.read_csv('movies.csv')
columns = data[['Title', 'Year', 'Score', 'Genre', 'Director', 'Runtime', 'Revenue']]

IMDB = pandas.DataFrame(data)

gen = IMDB['Genre'].str.contains("Comedy")
rank = IMDB.groupby(gen)['Score'].nlargest(10)
print(rank)

This code outputs the following result:

enter image description here

However, I want to instead pull the True section only and ignore the False section. I'm also trying to print the movie title with the corresponding score that shows in the result.
Any pointers on how I can approach this?


Solution

  • This should give you only the True records.

    rank[True]