将元组索引拆分为多索引pandas数据帧 - 使用python

时间:2017-07-14 12:41:01

标签: python pandas tuples

我从数据框列表中创建了一个数据框,如下所示:

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

我附上了输出图片。

我想转换 image

它是这样的:

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

1 个答案:

答案 0 :(得分:3)

您可以使用pd.MultiIndex.from_tuples设置索引。

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