pandas Python乘法时的多索引错误

jyztefdp  于 2023-06-20  发布在  Python
关注(0)|答案(1)|浏览(98)

我有一个 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有效
什么是最好的方法来做以上的跨非唯一多索引!

u3r8eeie

u3r8eeie1#

你想要的是模棱两可的,但我想这可能是:

scaled = caps.set_index(['Substation', 'DeviceID', 'Input'])['Capacity'].pipe(lambda s: s.max()/s)

raw.mul(scaled, axis=1)

输出:

Substation             SUB1                                                   
DeviceID             INV001           INV002          INV0023           INV003
Input      InverterA_Input1 InverterA_Input1 InverterA_Input1 InverterA_Input1
0                  1.072727         2.052174              NaN              NaN
1                  4.290909         5.130435              NaN              NaN

使用的输入(略有修改):

import pandas as pd

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'])

data = [[1,2,3], [4,5,6]]  # Example data for the DataFrame
raw = pd.DataFrame(data, columns=multi_index)

data = [
    ['SUB1', 'INV001', 'InverterA_Input1', 110],
    ['SUB1', 'INV002', 'InverterA_Input1', 115],
    ['SUB1', 'INV003', 'InverterA_Input1', 118]
]

caps = pd.DataFrame(data, columns=['Substation', 'DeviceID', 'Input', 'Capacity'])

相关问题