DMan
DMan

Reputation: 73

split index of tuples into multindex pandas dataframe - using python

I created a dataframe from a list of dataframes like this:

team_df = pd.concat(all_teams, keys=flat_list, axis=0)

I attached a picture of the output.

I want to convert image

it to is something like this:

team1

player 1
player 2
player 3
player 4
player 5
player 6

team2

player 1
player 2
player 3
player 4
player 5
player 6

Upvotes: 5

Views: 3128

Answers (1)

Zero
Zero

Reputation: 76917

You can use pd.MultiIndex.from_tuples to set index.

In [1045]: df.index = pd.MultiIndex.from_tuples(df.index, names=['team', 'player'])

In [1046]: df
Out[1046]:
             d
team player
1    2       1
     3       2
2    3       3

Upvotes: 7

Related Questions