0

I'm currently analysing a set of data and have columns named "Genre", "Actor" and "Rating".

With Genre as the main key, how should I go about acquiring the top 3 rated actors from each genre?

I've tried to initially sort the ratings in descending order first and then subsequently, taking the top 3 highest rated actors. However, this results in a jumbled mess when trying to parse it through


data1 = data.groupby(["genre0", "Actor0"])[["Rating"]].mean().sort_values("Rating", ascending=False)
data2 = data.groupby(["genre0", "Actor1"])[["Rating"]].mean().sort_values("Rating", ascending=False)
data3 = data.groupby(["genre0", "Actor2"])[["Rating"]].mean().sort_values("Rating", ascending=False)


allgenres = pd.concat([data1, data2, data3])

allgenres.groupby("genre0").head(3)

The output should reflect:

(Genre) - (Actors Names) - (Top 3 Rated Actors)

E.g.

Action - Actor A - 10
         Actor B - 9
         Actor C - 8

Animation - Actor D - 10
            Actor E - 9
            Actor F - 8

I do not wish to reset the index and keep the ordering as it is, but group the results together.

My Results

Transit
  • 3
  • 3

0 Answers0