数据:Here
问题: 我有几个数据表,我将它们作为数据帧导出到Python。我想对这些数据框执行乘法运算,这将生成另一个数据框,该数据框的尺寸与我使用的数据框相同,并且/或者基于所使用的不同数据框的组合来增加尺寸(即索引)。但是,我偶然发现了一些无法找到解决方案的问题。下面是代码。
代码:
#---------------------------------------------------------------------------------------------------
#Load the pandas library
#---------------------------------------------------------------------------------------------------
import numpy as np
import pandas as pd
#---------------------------------------------------------------------------------------------------
#Load the dataframes
#---------------------------------------------------------------------------------------------------
##Supply at the gridcell level (in Pj per year)
biosup = pd.read_excel('01EconMod_EU1.xlsx', sheet_name = 'biosup', skiprows = 5, index_col = 0, usecols = 'A:K')
##Cost at the gridcell level (in MEUR per Pj)
biocost = pd.read_excel('01EconMod_EU1.xlsx', sheet_name = 'biocost', skiprows = 5, index_col = 0, usecols = 'A:K')
##Demand at the gridcell level (in Pj per year)
biodem = pd.read_excel('01EconMod_EU1.xlsx', sheet_name = 'biodem', skiprows = 5, index_col = [0,1], usecols = 'A:L')
##Inter-gridcell distance matrix (in km)
dist = pd.read_excel('01EconMod_EU1.xlsx', sheet_name = 'distance', skiprows = 5, index_col = 0, usecols = 'A:AE')
#---------------------------------------------------------------------------------------------------
#Definition of model parameter
#---------------------------------------------------------------------------------------------------
##Power parameter for the distance-decay component (gamma)
gamma = pd.DataFrame({'sim1':[1.06],'sim2':[1.59],'sim3':[2.12]})
gamma = gamma.transpose()
gamma.columns = ['val']
##Inter-gridcell distance range for the supply curve determination (dmaxsup in km)
dmaxsup = pd.DataFrame({'dsup1':[390],'dsup2':[770],'dsup3':[1050]})
dmaxsup = dmaxsup.transpose()
dmaxsup.columns = ['dmax']
##Inter-gridcell distance range for the distance-decay (dmaxdem in km)
dmaxdem = pd.DataFrame({'ddem1':[750],'ddem2':[1000]})
dmaxdem = dmaxdem.transpose()
dmaxdem.columns = ['dmax']
#---------------------------------------------------------------------------------------------------
#New parameter calculation
#---------------------------------------------------------------------------------------------------
##The ratio of the inter-gridcell distance and the dmaxdem
dist1 = pd.DataFrame(np.concatenate(dist.values / dmaxdem.values[:, None]), pd.MultiIndex.from_product([dmaxdem.index, dist.index]), dist.columns)
##The decay coefficients
decay = pd.DataFrame(np.concatenate(2 * (1 / (1 + (np.exp(dist1.values)**gamma.values[:, None])))), pd.MultiIndex.from_product([gamma.index, dist1.index]), dist1.columns)
decay1 = pd.DataFrame(np.concatenate(2 * (1 / (1 + (np.exp(dist.values / dmaxdem.values[:, None])**gamma.values[:, None])))), pd.MultiIndex.from_product([dmaxdem.index, gamma.index, dist.index]), dist.columns)
对代码的评论:
1 /参数“ dist1”表示“ dist”数据帧被“ dmaxdem”数据帧的每个元素所除。认为“ dmaxdem”数据帧的值是距离方案。换句话说,此运算会计算出每个距离值的比率。
2 /我尝试计算距离衰减系数,即“衰减”数据帧,如方括号内的公式所定义。但是,我收到以下错误消息
NotImplementedError: isna is not defined for MultiIndex
我认为与“ dist1”数据帧的多索引结构有关。我已经尝试通过嵌入先前的操作来尝试一种直接方法,该方法将需要使用3个不同的数据帧,如“ decay1”代码所示。我收到以下错误
ValueError: operands could not be broadcast together with shapes (2,30,30) (3,1,1)
任何帮助将不胜感激。
答案 0 :(得分:0)
如果我误解了您,请原谅我,因为在发布答案之前我无法发表评论:
好吧,如果它们的长度相同,并且索引相同,则可以先将它们沿0轴连接起来,然后开始。这将创建一个更大的数据框。接下来,您可以声明一个或多个条件列:
largerdf = pd.concat([df1, df2, df3 , dfn], axis=0)
largerdf[“calculationcolumn”] = largerdf[“columnvalue1”] *largerdf[“columnvalue2”]
或将操作数更改为所需的任何值。