mydf = df1.groupby(['key', 'year']).size().reset_index()
mydf.rename(columns={(0): 'frequency'}, inplace=True)
mydf
Pandas: How can I remove duplicate rows from DataFrame and calculate their frequency?