Q:

How to club the common elements of the index in a dataframe?

I have a dataframe like this:
           type count
batsman         
V Kohli     4   361
V Kohli     6   149
SK Raina    5   1
SK Raina    6   161
RG Sharma   5   1
RG Sharma   6   164

Now we can see that there are commmon entries in the index. I want to plot a barh plot, so i want to club the index into one so that the dataframe looks something like this:
            type count
batsman         
V Kohli     4   361
            6   149
SK Raina    5   1
            6   161
RG Sharma   5   1
            6   164

How do i do that?

A:

You can assign the index to a new column and do a groupby.
df['batsman'] = df.index
grouped_df = df.groupby(['batsman', 'type']).agg({'count': 'sum'})
# grouped_df

           type count
batsman         
V Kohli     4   361
            6   149
SK Raina    5   1
            6   161
RG Sharma   5   1
            6   164

