6

demand複数の商品 (ここでは Water、Elec) とエリア タイプ (Com、Ind、Res) に与えられたルックアップ テーブル ( ) とareas、これらのエリア タイプのエリアのテーブルである DataFrame ( ) を乗算したいと考えています。

import pandas as pd
areas = pd.DataFrame({'Com':[1,2,3], 'Ind':[4,5,6]})
demand = pd.DataFrame({'Water':[4,3], 
                       'Elec':[8,9]}, index=['Com', 'Ind'])

前:

areas
   Com  Ind
0    1    4
1    2    5
2    3    6

demand
     Elec  Water
Com     8      4
Ind     9      3

後:

area_demands                  
     Com          Ind         
     Elec  Water  Elec  Water 
0       8      4    36     12 
1      16      8    45     15 
2      24     12    54     18 

私の試み

冗長で不完全です。は、任意の数の商品では機能しません。

areas = pd.DataFrame({'area': areas.stack()})
areas.index.names = ['Edge', 'Type']
both = areas.reset_index(1).join(demand, on='Type')
both['Elec'] = both['Elec'] * both['area']
both['Water'] = both['Water'] * both['area']
del both['area']
# almost there; it must be late, I fail to make 'Type' a hierarchical column...

もうすぐです:

     Type  Elec  Water
Edge
0     Com     8      4
0     Ind    36     12
1     Com    16      8
1     Ind    45     15
2     Com    24     12
2     Ind    54     18

要するに

DataFramesareasdemand適切な方法で結合/乗算するにはどうすればよいですか?

4

1 に答える 1