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

标签 python pandas tuples

我从这样的数据框列表中创建了一个数据框:

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

我附上了输出的图片。

我想转换
image

它是这样的:

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/

相关文章:

python - pyenv:python:找不到命令

python - 使用 Redis 与 memcached+db 作为 Django 的 session 系统的优缺点?

python - Peewee - 在运行时确定有关模型的元数据

python - 如果包含列表的列具有来自另一个较大列表的元素,如何输出 bool 值?

python - 应用循环后原始数据帧没有变化

python - Mypy 提示默认参数值与省略号不兼容

python - 如何使用 numpy 在线性时间内通过唯一值获取累积计数?

python - 如何使用列表推导将元组的元组转换为一维列表?

c++ - 为什么我的代码中 get<>() 分配的值会在构造之外发生变化?

python - python中字符串到元组的转换