引用数据透视表 table 数据帧中的多列位置

Referencing multi-column locations in a pivot table dataFrame

我有一些数据已导入到 dataFrame 中,我从中创建了一个多列数据透视表 table CokePepsi.pivot_table(index=['Date'], columns=['Brand', 'Type'], values=['cratesSold'], aggfunc='sum')

这是仅针对可口可乐筛选的结果数据透视表 table:

    cratesSold
Brand   Coke
Type    Diet    Regular
Date        
4/14/21 9928    10729
4/15/21 8526    8556
4/16/21 7556    7788
4/19/21 9709    9765
4/20/21 13234   13588
4/21/21 11946   11401
4/22/21 14407   14956
4/23/21 10956   11776
4/26/21 8324    7487
4/27/21 7705    8166
4/28/21 6771    6471
4/30/21 10988   10594
5/4/21  11351   11635
5/7/21  8809    9454
5/12/21 12081   13249
5/13/21 12674   11942
5/14/21 8729    10869
5/17/21 6217    6900
5/18/21 10502   8306
5/19/21 8990    11061
5/20/21 8210    8316

我想在此数据透视表中创建两个新列 table:一个包含 Coke(Diet) * -1 的值(即 Coke-Diet 列中所有值的负值)和另一个计算饮食和常规之间差异(即增量)的列:Coke(Regular) – Coke(Diet)

我知道如何将列添加到基本数据框以及 运行 使用基本数据框中其他列的数据进行计算,但是我正在努力寻找正确引用多列位置的语法枢轴 table.

更新:

根据@sammywemmy 的要求,我将数据作为字典包含在内。

[{' Date': '25-Jun-21', ' Time': '23:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2330.6}, {' Date': '25-Jun-21', ' Time': '22:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2330.43}, {' Date': '25-Jun-21', ' Time': '21:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2330.9}, {' Date': '25-Jun-21', ' Time': '20:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2330.6}, {' Date': '25-Jun-21', ' Time': '19:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2330.7}, {' Date': '25-Jun-21', ' Time': '18:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2331.0}, {' Date': '25-Jun-21', ' Time': '17:00:00', ' cratesSold': 5.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2331.5}, {' Date': '25-Jun-21', ' Time': '16:00:00', ' cratesSold': 4.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2331.97}, {' Date': '25-Jun-21', ' Time': '15:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2334.03}, {' Date': '25-Jun-21', ' Time': '14:00:00', ' cratesSold': 1.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.57}, {' Date': '25-Jun-21', ' Time': '13:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.95}, {' Date': '25-Jun-21', ' Time': '12:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2335.8}, {' Date': '25-Jun-21', ' Time': '11:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.8}, {' Date': '25-Jun-21', ' Time': '10:00:00', ' cratesSold': 5.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.8}, {' Date': '25-Jun-21', ' Time': '9:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2335.67}, {' Date': '25-Jun-21', ' Time': '8:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.47}, {' Date': '25-Jun-21', ' Time': '7:00:00', ' cratesSold': 4.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.4}, {' Date': '25-Jun-21', ' Time': '6:00:00', ' cratesSold': 6.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2334.72}, {' Date': '25-Jun-21', ' Time': '5:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2334.87}, {' Date': '25-Jun-21', ' Time': '4:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2334.77}, {' Date': '25-Jun-21', ' Time': '3:00:00', ' cratesSold': 8.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2334.63}, {' Date': '25-Jun-21', ' Time': '2:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2334.57}, {' Date': '25-Jun-21', ' Time': '1:00:00', ' cratesSold': 5.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2334.4}, {' Date': '25-Jun-21', ' Time': '0:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2334.27}, {' Date': '24-Jun-21', ' Time': '23:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2333.9}, {' Date': '24-Jun-21', ' Time': '22:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2333.8}, {' Date': '24-Jun-21', ' Time': '21:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2333.37}, {' Date': '24-Jun-21', ' Time': '20:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2330.6}, {' Date': '24-Jun-21', ' Time': '19:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2330.43}, {' Date': '24-Jun-21', ' Time': '18:00:00', ' cratesSold': 7.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2330.9}, {' Date': '24-Jun-21', ' Time': '17:00:00', ' cratesSold': 5.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2330.6}, {' Date': '24-Jun-21', ' Time': '16:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2330.7}, {' Date': '24-Jun-21', ' Time': '15:00:00', ' cratesSold': 1.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2331.0}, {' Date': '24-Jun-21', ' Time': '14:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2331.5}, {' Date': '24-Jun-21', ' Time': '13:00:00', ' cratesSold': 6.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2331.97}, {' Date': '24-Jun-21', ' Time': '12:00:00', ' cratesSold': 9.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.03}, {' Date': '24-Jun-21', ' Time': '11:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.57}, {' Date': '24-Jun-21', ' Time': '10:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.95}, {' Date': '24-Jun-21', ' Time': '9:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2335.27}, {' Date': '24-Jun-21', ' Time': '8:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.47}, {' Date': '24-Jun-21', ' Time': '7:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2335.4}, {' Date': '24-Jun-21', ' Time': '6:00:00', ' cratesSold': 8.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2335.22}, {' Date': '24-Jun-21', ' Time': '5:00:00', ' cratesSold': 3.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.3}, {' Date': '24-Jun-21', ' Time': '4:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.77}, {' Date': '24-Jun-21', ' Time': '3:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.93}, {' Date': '24-Jun-21', ' Time': '2:00:00', ' cratesSold': 4.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2335.07}, {' Date': '24-Jun-21', ' Time': '1:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2335.73}, {' Date': '24-Jun-21', ' Time': '0:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2336.73}, {' Date': '23-Jun-21', ' Time': '23:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2336.7}, {' Date': '23-Jun-21', ' Time': '22:00:00', ' cratesSold': 5.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2336.8}, {' Date': '23-Jun-21', ' Time': '21:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2336.8}, {' Date': '23-Jun-21', ' Time': '20:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2336.8}, {' Date': '23-Jun-21', ' Time': '19:00:00', ' cratesSold': 12.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2336.83}, {' Date': '23-Jun-21', ' Time': '18:00:00', ' cratesSold': 3.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2337.17}, {' Date': '23-Jun-21', ' Time': '17:00:00', ' cratesSold': 4.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2337.14}, {' Date': '23-Jun-21', ' Time': '16:00:00', ' cratesSold': 6.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2337.1}, {' Date': '23-Jun-21', ' Time': '15:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2336.97}, {' Date': '23-Jun-21', ' Time': '14:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2336.8}, {' Date': '23-Jun-21', ' Time': '13:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2336.6}, {' Date': '23-Jun-21', ' Time': '12:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2337.0}, {' Date': '23-Jun-21', ' Time': '11:00:00', ' cratesSold': 11.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2337.0}, {' Date': '23-Jun-21', ' Time': '10:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2336.6}, {' Date': '23-Jun-21', ' Time': '9:00:00', ' cratesSold': 7.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2336.8}, {' Date': '23-Jun-21', ' Time': '8:00:00', ' cratesSold': 5.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2336.97}, {' Date': '23-Jun-21', ' Time': '7:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2337.1}, {' Date': '23-Jun-21', ' Time': '6:00:00', ' cratesSold': 1.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2337.14}, {' Date': '23-Jun-21', ' Time': '5:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2337.17}, {' Date': '23-Jun-21', ' Time': '4:00:00', ' cratesSold': 6.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2336.83}, {' Date': '23-Jun-21', ' Time': '3:00:00', ' cratesSold': 9.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2336.8}, {' Date': '23-Jun-21', ' Time': '2:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2336.8}, {' Date': '23-Jun-21', ' Time': '1:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2336.8}, {' Date': '23-Jun-21', ' Time': '0:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.93}, {' Date': '22-Jun-21', ' Time': '23:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2334.77}, {' Date': '22-Jun-21', ' Time': '22:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2335.3}, {' Date': '22-Jun-21', ' Time': '21:00:00', ' cratesSold': 8.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2335.22}, {' Date': '22-Jun-21', ' Time': '20:00:00', ' cratesSold': 3.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2335.4}, {' Date': '22-Jun-21', ' Time': '19:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2335.47}, {' Date': '22-Jun-21', ' Time': '18:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.27}, {' Date': '22-Jun-21', ' Time': '17:00:00', ' cratesSold': 1.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.95}, {' Date': '22-Jun-21', ' Time': '16:00:00', ' cratesSold': 1.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.57}, {' Date': '22-Jun-21', ' Time': '15:00:00', ' cratesSold': 3.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2334.03}, {' Date': '22-Jun-21', ' Time': '14:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.03}, {' Date': '22-Jun-21', ' Time': '13:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Pepsi', ' cratePrice': 2334.57}, {' Date': '22-Jun-21', ' Time': '12:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2334.95}, {' Date': '22-Jun-21', ' Time': '11:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2335.27}, {' Date': '22-Jun-21', ' Time': '10:00:00', ' cratesSold': 8.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2335.47}, {' Date': '22-Jun-21', ' Time': '9:00:00', ' cratesSold': 3.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.4}, {' Date': '22-Jun-21', ' Time': '8:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2335.22}, {' Date': '22-Jun-21', ' Time': '7:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2335.3}, {' Date': '22-Jun-21', ' Time': '6:00:00', ' cratesSold': 4.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2334.77}, {' Date': '22-Jun-21', ' Time': '5:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.47}, {' Date': '22-Jun-21', ' Time': '4:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2335.27}, {' Date': '22-Jun-21', ' Time': '3:00:00', ' cratesSold': 3.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.95}, {' Date': '22-Jun-21', ' Time': '2:00:00', ' cratesSold': 5.0, ' Type': 'Diet', ' Brand': 'Pepsi', ' cratePrice': 2334.57}, {' Date': '22-Jun-21', ' Time': '1:00:00', ' cratesSold': 2.0, ' Type': 'Diet', ' Brand': 'Coke', ' cratePrice': 2334.03}, {' Date': '22-Jun-21', ' Time': '0:00:00', ' cratesSold': 2.0, ' Type': 'Regular', ' Brand': 'Coke', ' cratePrice': 2334.03}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}, {' Date': nan, ' Time': nan, ' cratesSold': nan, ' Type': nan, ' Brand': nan, ' cratePrice': nan}]

这是我想要实现的想法(在 excel 中产生),两个彩色列是根据现有数据透视列计算的新添加的列。

更新 2:

经过 大量 的搜索,我拼凑出一个部分有效的解决方案——但我希望能帮助解决剩余的错误。

使用一些简化的源数据:

np.random.seed(1)
df = pd.DataFrame(np.random.random((10, 12)))
df.columns = pd.MultiIndex.from_product(
    [
        ['one', 'two', 'three'],
        ['A', 'B', 'C', 'D']
    ]
)

我能够添加一个新列和子列,并根据现有的多索引列使用计算填充这些新列(从而解决了我最初问题的第一部分,即获取包含的新列现有列的负值):

df[('newMeta', 'newSub')] = df.loc[0:len(df.index), [('one', 'C')]] * -1

        one                      ...     three             newMeta
          A         B         C  ...         C         D    newSub
0  0.209685  0.719405  0.313690  ...  0.223464  0.177383 -0.313690
1  0.280559  0.788768  0.679686  ...  0.332770  0.866036 -0.679686
2  0.763944  0.990348  0.103789  ...  0.067546  0.134955 -0.103789
3  0.945861  0.220135  0.929363  ...  0.669494  0.743312 -0.929363
4  0.455665  0.903104  0.652752  ...  0.610537  0.887649 -0.652752
5  0.070355  0.401810  0.841703  ...  0.288189  0.711274 -0.841703
6  0.700615  0.213194  0.156292  ...  0.127861  0.363358 -0.156292
7  0.738146  0.878900  0.317653  ...  0.354384  0.492631 -0.317653
8  0.766122  0.518608  0.247643  ...  0.736120  0.952106 -0.247643
9  0.669162  0.474207  0.599781  ...  0.658298  0.913724 -0.599781

我同样能够创建另一个多索引列,它将现有多索引列的值添加到自身(shorthand:new multi-column [('Meta2', 'Sub2')] = [('one', 'C')] + [('one', 'C')]

df[('Meta2', 'Sub2')] = (df.loc[0:len(df.index), [('one', 'C')]]) + (df.loc[0:len(df.index), [('one', 'C')]])

        one                      ...     three   newMeta     Meta2
          A         B         C  ...         D    newSub      Sub2
0  0.209685  0.719405  0.313690  ...  0.177383 -0.313690  0.627380
1  0.280559  0.788768  0.679686  ...  0.866036 -0.679686  1.359372
2  0.763944  0.990348  0.103789  ...  0.134955 -0.103789  0.207578
3  0.945861  0.220135  0.929363  ...  0.743312 -0.929363  1.858727
4  0.455665  0.903104  0.652752  ...  0.887649 -0.652752  1.305503
5  0.070355  0.401810  0.841703  ...  0.711274 -0.841703  1.683407
6  0.700615  0.213194  0.156292  ...  0.363358 -0.156292  0.312584
7  0.738146  0.878900  0.317653  ...  0.492631 -0.317653  0.635306
8  0.766122  0.518608  0.247643  ...  0.952106 -0.247643  0.495286
9  0.669162  0.474207  0.599781  ...  0.913724 -0.599781  1.199561

然而,非常令人沮丧且看似无法解释的障碍是无法更改 单个 索引参考来自之前的 [('one', 'C')] + [('one', 'C')].

如果我尝试类似 [('one', 'C')] + [('two', 'C')] 的操作,我会遇到一系列错误(最后复制)。

谁能解释为什么 [('one', 'C')] + [('one', 'C')] 应该在成功添加的新列中产生正确计算的值,而 [('one', 'C')] + [('two', 'C')] 会引发错误?

这是官方调用的问题语句:

df[('Meta2', 'Sub2')] = (df.loc[0:len(df.index), [('one', 'C')]]) + (df.loc[0:len(df.index), [('two', 'C')]])

以及完整的错误列表:

Traceback (most recent call last):
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3081, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Meta2'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "pandas/_libs/index.pyx", line 705, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3083, in get_loc
    raise KeyError(key) from err
KeyError: 'Meta2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py", line 3826, in _set_item
    loc = self._info_axis.get_loc(key)
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/multi.py", line 2886, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 708, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
KeyError: ('Meta2', 'Sub2')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/user/Desktop/ScratchSpace/ExtractIce/multiIndexTests.py", line 156, in <module>
    df[('Meta2', 'Sub2')] = (df.loc[0:len(df.index), [('one', 'C')]]) + \
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py", line 3163, in __setitem__
    self._set_item(key, value)
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py", line 3243, in _set_item
    NDFrame._set_item(self, key, value)
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py", line 3829, in _set_item
    self._mgr.insert(len(self._info_axis), key, value)
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 1203, in insert
    block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1))
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 2751, in make_block
    return klass(values, ndim=ndim, placement=placement)
  File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 142, in __init__
    raise ValueError(
ValueError: Wrong number of items passed 2, placement implies 1

如果您将这两列转换为 numpy 数组而不是 pandas 数据帧,这应该可以解决您的问题。您可以使用以下内容:

df[('Meta2', 'Sub2')] = (df.loc[0:len(df.index), [('one', 'C')]]).to_numpy() \ + (df.loc[0:len(df.index), [('two', 'C')]]).to_numpy()

需要注意的重要部分是 .to_numpy() 函数。这会将列的值加在一起,而不是列本身。