I have the following df, which is adapted from here
import pandas as pd
df = pd.DataFrame({'group':[1,1,1,2,2,2,3,3,3],
'value':[1,2,3,1,2,3,4,3,2]})
I would like to have the following result:
df1 = pd.DataFrame({'group':[1,1,1,2,2,2,3,3,3],
'value':[0,2,3,0,2,3,4,3,0]})
The logic is (1) to select top 2 numbers for each group and (2) to set other values to 0.
Any suggestions? Thanks.