pandas 向多索引 Dataframe 添加另一级别的标头

nxagd54h  于 2022-12-02  发布在  其他
关注(0)|答案(1)|浏览(235)

我有以下 Dataframe :

dic = {'US':{'Quality':{'points':"-2 n", 'difference':'equal', 'stat': 'same'}, 'Prices':{'points':"-7 n", 'difference':'negative', 'stat': 'below'}, 'Satisfaction':{'points':"3 n", 'difference':'positive', 'stat': 'below'}},
      'UK': {'Quality':{'points':"3 n", 'difference':'equal', 'stat': 'above'}, 'Prices':{'points':"-13 n", 'difference':'negative', 'stat': 'below'}, 'Satisfaction':{'points':"2 n", 'difference':'negative', 'stat': 'same'}}}
d1 = defaultdict(dict)
for k, v in dic.items():
    for k1, v1 in v.items():
        for k2, v2 in v1.items():
            d1[(k, k2)].update({k1: v2})

df = pd.DataFrame(d1)

df.columns = df.columns.rename("Skateboard", level=0)
df.columns = df.columns.rename("Q3", level=1)
df.insert(loc=0, column=('', 'Mode'), value="Website")

目前,它看起来如下所示:

如何向多索引 Dataframe 添加另一层头,使其看起来像下图所示?

更新日期:

dic = {'US':{'Quality':{'points':"-2 n", 'difference':'equal', 'stat': 'same'}, 'Prices':{'points':"-7 n", 'difference':'negative', 'stat': 'below'}, 'Satisfaction':{'points':"3 n", 'difference':'positive', 'stat': 'below'}},
      'UK': {'Quality':{'points':"3 n", 'difference':'equal', 'stat': 'above'}, 'Prices':{'points':"-13 n", 'difference':'negative', 'stat': 'below'}, 'Satisfaction':{'points':"2 n", 'difference':'negative', 'stat': 'same'}}}
d1 = defaultdict(dict)
for k, v in dic.items():
    for k1, v1 in v.items():
        for k2, v2 in v1.items():
            d1[(k, k2)].update({k1: v2})

df = pd.DataFrame(d1)

df.columns = df.columns.rename("Skateboard", level=0)
df.columns = df.columns.rename("Metric", level=1)
df1 = df.xs('points', axis=1, level=1, drop_level=False)
df2 = df.drop('points', axis=1, level=1)
df3 = (pd.concat([df1, df2], keys=['GM', ''], axis=1)
 .swaplevel(0, 1, axis=1)
 .sort_index(axis=1))
df3.columns = df3.columns.rename("Q3", level=1)
df3.insert(loc=0, column=('','', 'Mode'), value="Website")

df3

现在数据框如下所示:

如何将标题GM移动到US和UK列的第一个位置(请参见最终输出的第二个图像)?

hwamh0ep

hwamh0ep1#

示例

data = {('A', 'a'): {0: 8, 1: 3, 2: 4},
        ('A', 'b'): {0: 5, 1: 7, 2: 8},
        ('A', 'c'): {0: 1, 1: 7, 2: 6},
        ('B', 'a'): {0: 7, 1: 1, 2: 0},
        ('B', 'b'): {0: 1, 1: 1, 2: 7},
        ('B', 'c'): {0: 7, 1: 7, 2: 4}}
df = pd.DataFrame(data)

df

A           B
    a   b   c   a   b   c
0   8   5   1   7   1   7
1   3   7   7   1   1   7
2   4   8   6   0   7   4

代码

创建新的级别,并将c添加到a列,添加da除外)
具有a的df(df1

df1 = df.xs('a', axis=1, level=1, drop_level=False)

输出(df1):

A   B
    a   a
0   8   7
1   3   1
2   4   0

adf2)之外的df

df2 = df.drop('a', axis=1, level=1)

输出(df2):

A       B
    b   c   b   c
0   5   1   1   7
1   7   7   1   7
2   8   6   7   4

将df1和df2与key连接

pd.concat([df1, df2], keys=['c', 'd'], axis=1)

输出:

c       d
    A   B   A       B
    a   a   b   c   b   c
0   8   7   5   1   1   7
1   3   1   7   7   1   7
2   4   0   8   6   7   4

swap级别和排序

(pd.concat([df1, df2], keys=['c', 'd'], axis=1)
 .swaplevel(0, 1, axis=1)
 .sort_index(axis=1))

实验结果:

A           B
    c   d       c   d
    a   b   c   a   b   c
0   8   5   1   7   1   7
1   3   7   7   1   1   7
2   4   8   6   0   7   4

我们可以向列添加级别
或采用简单的方式

df3 = pd.concat([df], keys=[''], names=['Q3'], axis=1).swaplevel(0, 1, axis=1)
df3.columns = df3.columns.map(lambda x: (x[0], 'c', x[2]) if x[2] == 'a' else x)

df3

A           B
Q3  c           c   
    a   b   c   a   b   c
0   8   5   1   7   1   7
1   3   7   7   1   1   7
2   4   8   6   0   7   4

相关问题