There is a Pandas dataframe: index title rating year decade 0 0 "BLOW THE NIGHT!" Let's Spend the Night Togeth... 3.000000 1983 1980-1990 1 1 "Great Performances" Cats (1998) 2.896648 1998 1990-2000 2 2 #1 Cheerleader Camp (2010) 2.111111 2010 2010-2020 3 3 #Captured (2017) 3.750000 2017 2010-2020 4 4 #Female Pleasure (2018) 3.666667 2018 2010-2020 ... ... ... ... ... ... 58953 58953 …And the Fifth Horseman Is Fear (1965) 3.250000 1965 1960-1970 58954 58954 キサラギ (2007) 3.250000 2007 2000-2010 58955 58955 チェブラーシカ (2010) 3.166667 2010 2010-2020 58956 58956 貞子3D (2012) 2.375000 2012 2010-2020 58957 58957 줄탁동시 (2012) 4.000000 2012 2010-2020 Write a function that groups titles by decade and calculates the percentage of titles with a rating of 5.0 relative to all titles in that decade
def group_by_rating(df): grouped = df.groupby('decade') result = {} for decade, group in grouped: result[decade] = (group.rating == 5.0).sum() / len(group) return result