我从数据框列表中创建了一个数据框,如下所示:
team_df = pd.concat(all_teams, keys=flat_list, axis=0)
我附上了输出图片。
我想转换
它是这样的:
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
答案 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