代码之家  ›  专栏  ›  技术社区  ›  8-Bit Borges

Pandas-压平柱

  •  0
  • 8-Bit Borges  · 技术社区  · 4 年前

    之后:

    1. 聚合 sum()
    2. 分组依据 ['country', 'match_id']
    3. 创建平均列 mean(axis=1)

    我的结论是:

                         Gls  Ast  avg_attack 
                         sum  sum 
    country    match_id
    Argentina  20eb96e2  0.10 0.20 0.15
               18eb43e2  0.20 0.30 0.25
    ...
    

    现在,我如何将我的数据帧展平为这个?

    country     match_id Gls  Ast  avg_attack 
    Argentina  20eb96e2  0.10 0.20 0.15
    Argentina  18eb43e2  0.20 0.30 0.25
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   abokey    4 年前

    使用 pandas.Index.get_level_values 要在列中展平分层索引,请 pandas.DataFrame.reset_index 以重置行中的多索引。

    df.columns = df.columns.get_level_values(0)
    out = df.reset_index()
    

    #输出:

    print(out)
    
         Country  match_id  Gls   Ast   avg_attack
    0  Argentina  20eb96e2  0.10  0.20        0.15
    1  Argentina  18eb43e2  0.20  0.30        0.25