【问题标题】:split index of tuples into multindex pandas dataframe - using python将元组的索引拆分为多索引熊猫数据框 - 使用 python
【发布时间】:2017-07-14 12:41:01
【问题描述】:

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

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

【问题讨论】:

    标签: python pandas tuples


    【解决方案1】:

    您可以使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-13
      • 2021-11-11
      • 2019-08-06
      • 1970-01-01
      • 2020-01-14
      • 2012-12-27
      • 2018-02-07
      • 2022-07-10
      相关资源
      最近更新 更多