I have data that is grouped and has a value associated with it. I'm fine with working out the rank of each value within its subgroup -
df['Rank'] = df.groupby('Group')['Value'].rank(ascending=True)
However, I'd also like to create an additional column that shows the top-ranked value and second-ranked value within each group as a separate column - i.e. in the image attached below (I've not worked out how to draw a table on this website yet!). Many thanks.