'How to fill in gaps of duplicate indices in dataframe? [duplicate]

I have a dataframe like as shown below

tdf = pd.DataFrame({'grade': np.random.choice(list('AAAD'),size=(5)),
                   'dash': np.random.choice(list('PPPS'),size=(5)),
                   'dumeel': np.random.choice(list('QWRR'),size=(5)),
                   'dumma': np.random.choice((1234),size=(5)),
                   'target': np.random.choice([0,1],size=(5))
})

I am trying to create a multi-index dataframe using some of the input columns

So, I tried the below

tdf.set_index(['grade','dumeel'],inplace=True)

However, this results in missing/gap for duplicate entries (in red highlight)

enter image description here

How can I avoid that and show my dataframe with all indices (whether it is duplicate or not)

I would like to my output to have all rows with corresponding indices based on original dataframe



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source