I would like to rank a column called Number after grouping by the column ID but the ranking would be done only if Variable_1 is True. If Variable_1 is False, assign directly to the new column Rank the highest rank : 1
data={'ID':["A","A","B","C","D","D"], 'Variable_1':[True,True,False,False,True,True], 'Number': [10,20,5,6,100,90]}
df=pd.DataFrame(data)
I tried to do the following but did not work:
df["Rank"] = np.where(df.Variable_1, df.groupby(["ID"])[["Number"]].rank(method='dense', ascending=False), 1)
This is the expected output:
