【问题标题】:Converting and comparing python multiindex dataframes转换和比较 python 多索引数据帧
【发布时间】:2021-03-31 16:47:48
【问题描述】:

我正在尝试比较两个数据框,寻找主题之间的不同值。其中一个数据框具有多索引列,其中包含比较所需的数据。下面是一个示例

weight = [1,5,2,4]
price = [2,6,3,5]
item = ['A','B','A','B']
date = ['20-12-2020', '21-12-2020', '20-12-2020', '21-12-2020']

DF2 = pd.DataFrame({'Date':date, 'weight':weight, 'price':price, 'item':item})


tuples = (['A', 'weight'], ['A', 'price'], ['B', 'weight'], ['B', 'price'])
index = pd.MultiIndex.from_tuples(tuples)

DF1 = pd.DataFrame(columns = index)

DF1['A','weight'] = [1,2]
DF1['A', 'price'] = [2,3]
DF1['B', 'weight'] = [5,4]
DF1['B', 'price'] = [6,5]
DF1.rename(index={0:'20-12-2020', 1:'21-12-2020'})

我们的目的是发现 DF1 和 DF2 之间给定项目和日期的重量和价格差异,但我不知道如何进行,因为 DF1 中的多索引也包含必要的数据,因为它包含项目。

【问题讨论】:

    标签: python pandas multi-index


    【解决方案1】:

    最简单的方法是:

    DF1[('C','diff weight')] = (DF1[('A','weight')] - DF1[('B','weight')])
    DF1[('C','diff price')] = (DF1[('A','price')] - DF1[('B','price')])
    

    这是

          A            B                 C           
      weight price weight price diff weight diff price
    0      1     2      5     6          -4         -4
    1      2     3      4     5          -2         -2
    

    或者

    DF1 = DF1['A'] - DF1['B']
    DF1.columns = pd.MultiIndex.from_tuples([('diff',col) for col in DF1.columns])
    

    给了

        diff      
      weight price
    0     -4    -4
    1     -2    -2
    

    【讨论】:

      猜你喜欢
      • 2016-10-17
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多