How do I multiply a column in a MultiIndex
dataframe with several scalars (from another dataframe)?
With normal dataframes it's fairly straightforward, but I get confused when it gets to MultiIndex dataframes. Any recommendations?
Dummy data:
import pandas as pd
import numpy as np
def mklbl(prefix, n):
return ["%s%s" % (prefix, i) for i in range(n)]
miindex = pd.MultiIndex.from_product([mklbl('C', 4), mklbl('D', 2)])
micolumns = pd.MultiIndex.from_tuples([('Baseline', 'Electricity'), ('Baseline', 'Gas'), ('Consumption', 'Electricity'), ('Consumption', 'Gas')])
df = pd.DataFrame(np.arange(len(miindex) * len(micolumns))
.reshape((len(miindex), len(micolumns))),
index=miindex,
columns= micolumns)
cost = pd.DataFrame([['Electricity', 0.12],['Gas', 0.03]], columns=['fuel__name', 'cost_per_unit'])
Expected output is the dummy data multiplied by the corresponding cost_per_unit
for Electricity
and Gas
, (i.e. cost of energy use)