Python pandas:groupby一级MultiIndex,但仍然是其他级别

时间:2018-05-31 12:22:26

标签: python pandas

假设我有一个DataFrame:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.arange(0, 24).reshape((3, 8)))
df.columns = pd.MultiIndex.from_arrays([
    ['a1', 'a1', 'a2', 'a2', 'b1', 'b1', 'b2', 'b2'],
    ['4th', '5th', '4th', '5th', '4th', '5th', '4th', '5th']
])
print(df)

输出:

       a1      a2      b1      b2    
  4th 5th 4th 5th 4th 5th 4th 5th
0   0   1   2   3   4   5   6   7
1   8   9  10  11  12  13  14  15
2  16  17  18  19  20  21  22  23

我想通过一个词典分组:

label_dict = {'a1': 'A', 'a2': 'A', 'b1': 'B', 'b2': 'B'}
res = df.groupby(label_dict, axis=1, level=0).sum()
print(res)

输出:

    A   B
0   6  22
1  38  54
2  70  86

但我想要的是:

    A   A   B   B
  4th 5th 4th 5th
0   2   4  10  12
1  18  21  26  28
2  34  36  42  44

有什么想法吗?谢谢!

3 个答案:

答案 0 :(得分:10)

rename列中的sum级中,MultiIndexlabel_dict = {'a1': 'A', 'a2': 'A', 'b1': 'B', 'b2': 'B'} res = df.rename(columns=label_dict, level=0).sum(level=[0,1], axis=1) #alternative with groupby #res = df.rename(columns=label_dict, level=0).groupby(level=[0,1], axis=1).sum() print(res) A B 4th 5th 4th 5th 0 2 4 10 12 1 18 20 26 28 2 34 36 42 44 一起使用

<template>
    <div class="itemGenerate">
        <div>

            <ul>
                <li v-for="identifier in identifiers" :key="identifier">
                <input type="text" value=""/>{{identifier.name}}
                </li>
            </ul>

            <button type="button">Add</button>
        </div>

    </div>
</template>

<script>
export default {
  data() {
    return {
      identifiers: [{ name: "Flavour" }, { name: "Size" }, { name: "Color" }]
    };
  }
};

答案 1 :(得分:3)

使用数据帧重塑的另一种方法。

多索引列的堆栈级别1然后使用字典对列进行分组并取消堆栈以将多索引列添加回分组列。

SOCKS5

输出:

df.stack(1).groupby(label_dict, axis=1).sum().unstack()

答案 2 :(得分:0)

与现有解决方案类似,只是将它放在那里:

res = df.rename_axis(label_dict, axis=1).groupby(level=[0,1], axis=1).sum()
res
#    A       B    
#  4th 5th 4th 5th
#0   2   4  10  12
#1  18  20  26  28
#2  34  36  42  44