对多索引数据透视表 pandas 进行排序

Posted

技术标签:

【中文标题】对多索引数据透视表 pandas 进行排序【英文标题】:Sort multiindex pivot table pandas 【发布时间】:2020-01-07 21:55:06 【问题描述】:

想要按值对 pandas 枢轴进行排序。

data = 'Counterparty': 0: 'A',
  1: 'B',
  2: 'B',
  3: 'A',
  4: 'A',
  5: 'C',
  6: 'D',
  7: 'E',
  8: 'E',
  9: 'C',
  10: 'F',
  11: 'C',
  12: 'C',
  13: 'G',
 'Contract': 0: 'A1',
  1: 'B1',
  2: 'B2',
  3: 'A2',
  4: 'A3',
  5: 'C1',
  6: 'D1',
  7: 'E1',
  8: 'E2',
  9: 'C2',
  10: 'F1',
  11: 'C3',
  12: 'C4',
  13: 'G',
 'Delivery': 0: '1/8/2019',
  1: '1/8/2019',
  2: '1/8/2019',
  3: '1/8/2019',
  4: '1/8/2019',
  5: '1/8/2019',
  6: '1/8/2019',
  7: '1/8/2019',
  8: '1/8/2019',
  9: '1/8/2019',
  10: '1/8/2019',
  11: '1/8/2019',
  12: '1/8/2019',
  13: '1/8/2019',
 'Price': 0: 134.0,
  1: 151.0,
  2: 149.0,
  3: 134.0,
  4: 132.14700000000002,
  5: 150.0,
  6: 134.566,
  7: 153.0,
  8: 151.0,
  9: 135.0,
  10: 149.0,
  11: 135.0,
  12: 147.0,
  13: 151.0,
 'Balance': 0: 200.0,
  1: 54.87,
  2: 200.0,
  3: 133.44,
  4: 500.0,
  5: 500.0,
  6: 1324.05,
  7: 279.87,
  8: 200.0,
  9: 20.66,
  10: 110.15,
  11: 100.0,
  12: 100.0,
  13: 35.04

从数据中获取df: df = pd.DataFrame(data)

定义一个函数来计算价格的加权平均值

wa = lambda x: np.average(x, weights=df.loc[x.index, 'Balance'])

pivot = df.pivot_table(
    index=['Counterparty', 'Contract'],
    columns='Delivery',
    values=['Balance', 'Price'],
    aggfunc=
        'Balance': sum,
        'Price': wa
    ,
    margins=True
).fillna('').swaplevel(0,1,axis=1).sort_index(axis=1).round(3)

结果:

Delivery               1/8/2019          1/9/2019           All         
                       Balance    Price  Balance    Price   Balance    Price
Counterparty Contract                                                      
A            A1            200      134                     200.00  134.000
             A2         133.44      134                     133.44  134.000
             A3            500  132.147                     500.00  132.147
B            B1          54.87      151                      54.87  151.000
             B2            200      149                     200.00  149.000
C            C1            500      150                     500.00  150.000
             C2                            20.66      135    20.66  135.000
             C3            100      135                     100.00  135.000
             C4                              100      147   100.00  147.000
D            D1                          1324.05  134.566  1324.05  134.566
E            E1         279.87      153                     279.87  153.000
             E2            200      151                     200.00  151.000
F            F1         110.15      149                     110.15  149.000
G            G           35.04      151                      35.04  151.000
All                    2313.37  143.541  1444.71  135.433  3758.08  140.424

检查多索引的列顺序:

MultiIndex([('1/8/2019', 'Balance'),
            ('1/8/2019',   'Price'),
            ('1/9/2019', 'Balance'),
            ('1/9/2019',   'Price'),
            (     'All', 'Balance'),
            (     'All',   'Price')],
           name=['Delivery', 'None'])

重命名多索引并尝试通过传入包含元组的列表进行排序(参考:Multi Index Sorting in Pandas):

pivot.columns.name = ['Delivery', 'Metrics']

MultiIndex([('1/8/2019', 'Balance'),
            ('1/8/2019',   'Price'),
            ('1/9/2019', 'Balance'),
            ('1/9/2019',   'Price'),
            (     'All', 'Balance'),
            (     'All',   'Price')],
           name=['Delivery', 'Metrics'])

pivot.sort_values(by=[('Metrics', 'Delivery')], ascending=False)

导致密钥错误。

期望的结果:

Delivery               1/8/2019          1/9/2019          All         
                       Balance    Price  Balance    Price  Balance    Price
Counterparty Contract                                                      
C            C1        500      150                        500.00  150.000
A            A3        500      132.147                    500.00  132.147
E            E1        279.87   153                        279.87  153
...

【问题讨论】:

【参考方案1】:

首先删除 fillna 以避免混合值数字和字符串,然后按从 MultiIndex 创建的元组排序,而不是按 MultiIndex.columns.names。最后如果需要All行到最后一行添加concat

df = pd.DataFrame(data)
#print (df)

wa = lambda x: np.average(x, weights=df.loc[x.index, 'Balance'])

pivot = df.pivot_table(
    index=['Counterparty', 'Contract'],
    columns='Delivery',
    values=['Balance', 'Price'],
    aggfunc=
        'Balance': sum,
        'Price': wa
    ,
    margins=True
).swaplevel(0,1,axis=1).sort_index(axis=1).round(3)
pivot.columns.name = ['Delivery', 'Metrics']

df = pivot.sort_values(by=[('1/8/2019', 'Balance'), ('1/8/2019', 'Price')], ascending=False)

df = pd.concat([df.iloc[1:], df.iloc[[0]]])

print (df)
Delivery              1/8/2019          1/9/2019               All         
                       Balance    Price  Balance    Price  Balance    Price
Counterparty Contract                                                      
C            C1         500.00  150.000      NaN      NaN   500.00  150.000
A            A3         500.00  132.147      NaN      NaN   500.00  132.147
E            E1         279.87  153.000      NaN      NaN   279.87  153.000
             E2         200.00  151.000      NaN      NaN   200.00  151.000
B            B2         200.00  149.000      NaN      NaN   200.00  149.000
A            A1         200.00  134.000      NaN      NaN   200.00  134.000
             A2         133.44  134.000      NaN      NaN   133.44  134.000
F            F1         110.15  149.000      NaN      NaN   110.15  149.000
C            C3         100.00  135.000      NaN      NaN   100.00  135.000
B            B1          54.87  151.000      NaN      NaN    54.87  151.000
G            G           35.04  151.000      NaN      NaN    35.04  151.000
C            C2            NaN      NaN    20.66  135.000    20.66  135.000
             C4            NaN      NaN   100.00  147.000   100.00  147.000
D            D1            NaN      NaN  1324.05  134.566  1324.05  134.566
All                    2313.37  143.541  1444.71  135.433  3758.08  140.424

如果需要按所有列排序:

df = pivot.sort_values(by=pivot.columns.tolist(), ascending=False)

df = pd.concat([df.iloc[1:], df.iloc[[0]]])
print (df)
Delivery              1/8/2019          1/9/2019               All         
                       Balance    Price  Balance    Price  Balance    Price
Counterparty Contract                                                      
C            C1         500.00  150.000      NaN      NaN   500.00  150.000
A            A3         500.00  132.147      NaN      NaN   500.00  132.147
E            E1         279.87  153.000      NaN      NaN   279.87  153.000
             E2         200.00  151.000      NaN      NaN   200.00  151.000
B            B2         200.00  149.000      NaN      NaN   200.00  149.000
A            A1         200.00  134.000      NaN      NaN   200.00  134.000
             A2         133.44  134.000      NaN      NaN   133.44  134.000
F            F1         110.15  149.000      NaN      NaN   110.15  149.000
C            C3         100.00  135.000      NaN      NaN   100.00  135.000
B            B1          54.87  151.000      NaN      NaN    54.87  151.000
G            G           35.04  151.000      NaN      NaN    35.04  151.000
D            D1            NaN      NaN  1324.05  134.566  1324.05  134.566
C            C4            NaN      NaN   100.00  147.000   100.00  147.000
             C2            NaN      NaN    20.66  135.000    20.66  135.000
All                    2313.37  143.541  1444.71  135.433  3758.08  140.424

【讨论】:

非常感谢@jezrael .. 我现在明白使用多索引中的元组对列进行排序.. 但是,在对整个 df 进行排序时(还有几个月),有些月份没有正确排序。你介意我们在聊天中继续讨论吗? @yongsheng - 好的,可以邀请我吗?

以上是关于对多索引数据透视表 pandas 进行排序的主要内容,如果未能解决你的问题,请参考以下文章

具有多索引的 Pandas 子数据透视表和总数据透视表

具有多索引的 Pandas 数据透视表小计

结合 pandas 数据透视表多索引标题 - 一个时间戳,一个字符串

pandas 透视多索引列

多索引数据框到带有新列的数据透视表

pandas:如何使用多索引运行数据透视?