私は2つのパンダDataFramesを持っています-列weight
に単純なインデックスがありLand Use
ます。concentration
とにMultiIndexがLand Use
ありParameter
ます。
import pandas
from io import StringIO
conc_string = StringIO("""\
Land Use,Parameter,1E,1N,1S,2
Airfield,BOD5 (mg/l),0.418,0.118,0.226,1.063
Airfield,Ortho P (mg/l),0.002,0.001,0.001,0.002
Airfield,TSS (mg/l),1.773,11.47,0.862,0.183
Airfield,Zn (mg/l),0.001,0.001,4.95E-05,0.001
"Commercial",BOD5 (mg/l),0.036,0.0419,,0.315
"Commercial",Cu (mg/l),4.37E-05,7.34E-05,,0.00039
"Commercial",O&G (mg/l),0.0385,0.127,,0.263
Open Space,TSS (mg/l),0.371,3.01,1.209,0.147
Open Space,Zn (mg/l),0.0127,0.0069,0.0132,0.007
"Parking Lot",BOD5 (mg/l),0.924,0.0668,2.603,3.19
"Parking Lot",O&G (mg/l),1.02,0.149,1.347,1.88
"Rooftops",BOD5 (mg/l),0.135,1.00,0.0562,0.310""")
weight_string = StringIO("""\
Land Use,1E,1N,1S,2
Airfield,0.511,0.0227,0.0616,0.394
Commercial,0.0005,0.1704,0,0.1065
Open Space,0.0008,0.005,0.0002,0.0004
"Parking Lot",0.33,0.514,0.252,0.171
Rooftops,0.081,0.028,8.50E-05,0.003""")
concentration = pandas.read_csv(conc_string, index_col=[0,1])
weight = pandas.read_csv(weight_string, index_col=0)
この場合、カラム(1E、1N、1S、および2)は流域です。
私がやりたいのはParameter
、流域(列名)との重みで独立してすべての濃度を除算することですLand Use
。
ここではあまり運がありません。concentration / weight
確かに動作しません。データフレームをスタックして参加する運があまりありません
wstk = pandas.DataFrame(weight.stack())
wstk.index.names = ['Land Use', 'Basin']
wstk.rename(columns={0:'weight'}, inplace=True)
cstk = pandas.DataFrame(concentration.stack())
cstk.index.names = ['Land Use', 'Parameter', 'Basin']
cstk.rename(columns={0:'concentration'}, inplace=True)
wstk.join(cstk, on=['Land Use', 'Basin']) # fails
cstk.join(wstk, on=['Land Use', 'Basin']) # fails
on
最後の2行では、 kwargを省略してもエラーは発生しませんがNaN
、結合された列の結果が返されます。スタックされた両方のDataFrameにインデックスをドロップした場合も失敗します(たとえば、wstk.reset_index(inplace=True)
結合の前にドロップします)。
助言がありますか?