【问题标题】:Pandas Pivot Table formatting column namesPandas Pivot Table 格式化列名
【发布时间】:2016-08-20 03:55:21
【问题描述】:

我在 pandas 数据帧上使用了 pandas.pivot_table 函数,我的输出看起来与此类似:

                    Winners                 Runnerup            
         year       2016    2015    2014    2016    2015    2014
Country  Sport                              
india    badminton                              
india    wrestling  

我真正需要的是下面这样的东西

Country Sport   Winners_2016    Winners_2015    Winners_2014    Runnerup_2016   Runnerup_2015   Runnerup_2014
india   badminton   1   1   1   1   1   1
india   wrestling   1   0   1   0   1   0

我有很多专栏和年份,所以我无法手动编辑它们,所以谁能告诉我如何做到这一点?

【问题讨论】:

    标签: python pandas dataframe pivot-table data-munging


    【解决方案1】:

    你也可以使用列表推导:

    df.columns = ['_'.join(col) for col in df.columns]
    print (df)
                       Winners_2016  Winners_2015  Winners_2014  Runnerup_2016  \
    Country Sport                                                                
    india   badminton             1             1             1              1   
            wrestling             1             1             1              1   
    
                       Runnerup_2015  Runnerup_2014  
    Country Sport                                    
    india   badminton              1              1  
            wrestling              1              1  
    

    转换columnsto_series然后调用join的另一种解决方案:

    df.columns = df.columns.to_series().str.join('_')
    print (df)
                       Winners_2016  Winners_2015  Winners_2014  Runnerup_2016  \
    Country Sport                                                                
    india   badminton             1             1             1              1   
            wrestling             1             1             1              1   
    
                       Runnerup_2015  Runnerup_2014  
    Country Sport                                    
    india   badminton              1              1  
            wrestling              1              1  
    

    我对时间安排非常感兴趣:

    In [45]: %timeit ['_'.join(col) for col in df.columns]
    The slowest run took 7.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 4.05 µs per loop
    
    In [44]: %timeit ['{}_{}'.format(x,y) for x,y in zip(df.columns.get_level_values(0),df.columns.get_level_values(1))]
    The slowest run took 4.56 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000 loops, best of 3: 131 µs per loop
    
    In [46]: %timeit df.columns.to_series().str.join('_')
    The slowest run took 4.31 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000 loops, best of 3: 452 µs per loop
    

    【讨论】:

    • 真的很感兴趣 - 第一个列表理解要快 30 倍。
    • 是的,这真的很有帮助,因为我正在处理更大的数据集。谢谢!
    • 旧 Q,但当嵌套列名之一不是字符串时,我建议 ['_'.join(map(str, col)) for col in df.columns] 保持稳健性。
    【解决方案2】:

    试试这个:

    df.columns=['{}_{}'.format(x,y) for x,y in zip(df.columns.get_level_values(0),df.columns.get_level_values(1))]
    

    get_level_values 是您只需要获得结果多索引的一个级别。

    附注:您可以尝试按原样处理数据。我真的很讨厌 pandas multiIndex 很长一段时间,但它在我身上长大了。

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多