我从这样的数据框列表中创建了一个数据框:
team_df = pd.concat(all_teams, keys=flat_list, axis=0)
我附上了输出的图片。
我想转换
它是这样的:
1队
player 1
player 2
player 3
player 4
player 5
player 6
2队
player 1
player 2
player 3
player 4
player 5
player 6
最佳答案
您可以使用 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
关于python - 将元组索引拆分为多索引 Pandas 数据帧 - 使用 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45103255/