When group a pandas dataframe by one column say \"version\" and which has 10 distinct versions. How can one plot the Top 3 (which cover over 90%) and put the small remainders in
# number of top-n you want
n = 2
# group by & sort descending
df_sorted = (df
.groupby('Version').sum()
.sort_values('Value', ascending=False)
.reset_index()
)
# rename rows other than top-n to 'Others'
df_sorted.loc[df_sorted.index >= n, 'Version'] = 'Others'
# re-group by again
df_sorted.groupby('Version').sum()