我有一个 Dataframe ,我试图在多索引上相乘。
df 1 =raw是多索引
df2=caps
#df1
import pandas as pd
# Create the multi-index
index_values = [('SUB1', 'INV001', 'InverterA_Input1'), ('SUB1', 'INV002', 'InverterA_Input1'), ('SUB1', 'INV0023', 'InverterA_Input1')]
multi_index = pd.MultiIndex.from_tuples(index_values, names=['Substation', 'DeviceID', 'Input'])
# Create the DataFrame with the multi-index columns
data = [[112, 74.22, 83.15], [113.59, 79.94, 83.9]] # Example data for the DataFrame
raw = pd.DataFrame(data, columns=multi_index)
然后呢
#df2
import pandas as pd
# Create the data for the DataFrame
data = [
['SUB1', 'INV001', 'Input1', 'INV001A', 110],
['SUB1', 'INV001', 'Input1', 'INV001A', 115],
['SUB1', 'INV001', 'Input1', 'INV001A', 118]
]
# Create the DataFrame
caps = pd.DataFrame(data, columns=['Substation', 'DeviceID', 'Input', 'AnalysisGroup', 'Capacity'])
我正在尝试使用以下代码将caps caps["Capacity"].max() / caps["Capacity"]
乘以raw:
scaled = raw.multiply(
caps["Capacity"].max() / caps["Capacity"], axis=1
).copy
#I keep getting errors: ValueError: multiple levels only valid with MultiIndex
我也试过:
multiplied = raw.groupby(level=[0, 1]).apply(lambda group: group.multiply(group['Capacity'].max() / group['Capacity']))
# I get errors ValueError: multiple levels only valid with MultiIndex
并获取错误ValueError:多个级别仅对MultiIndex有效
什么是最好的方法来做以上的跨非唯一多索引!
1条答案
按热度按时间u3r8eeie1#
你想要的是模棱两可的,但我想这可能是:
输出:
使用的输入(略有修改):