简体   繁体   English

循环遍历Python中不同数据集的行列

[英]Loop through rows and columns of different data sets in Python

I am new to Python and am struggling to loop through the rows and columns of two different datasets, to generate an array of values.我是 Python 的新手,我正在努力循环遍历两个不同数据集的行和列,以生成一个值数组。

I have two dataframes (parameterMatrix and growthRates);我有两个数据框(parameterMatrix 和growthRates); one shows an array of species and the strengths of their interactions, and the other shows the growth rate of each species.一个显示了一系列物种及其相互作用的强度,另一个显示了每个物种的生长速度。

parameterMatrix :参数矩阵

             herbivores  youngScrub  matureScrub  sapling  matureTree  grassHerbs
herbivores          0.0        0.02            0     0.05           0         0.5
youngScrub         -0.2        0.00            0     0.00           0         0.0
matureScrub         0.0        0.00            0     0.00           0         0.0
sapling            -0.2        0.00            0     0.00           0         0.0
matureTree          0.0        0.00            0     0.00           0         0.0
grassHerbs         -5.0        0.00            0     0.00           0         0.0

growthRates :增长率

herbivores  youngScrub  matureScrub  sapling  matureTree  grassHerbs
0         0.1           0          0.1      0.1           0         0.2

I am trying to generate an array of values for each of the six species, that can ultimately be used to calculate the rate of change in each species over time.我正在尝试为六个物种中的每一个生成一组值,最终可以用来计算每个物种随时间的变化率。 I have manually written out each of the equations (see below), but I am wondering if there is a faster way to do this, eg by looping through each of these data frames.我已经手动写出了每个方程(见下文),但我想知道是否有更快的方法来做到这一点,例如通过循环遍历每个数据帧。

def ecoNetwork(X, t=0):

    num_herbivores = X[0]
    num_youngScrub = X[1]
    num_matureScrub = X[2]
    num_sapling = X[3]
    num_matureTree = X[4]
    num_grassHerb = X[5]

return np.array([ 

        # herbivores
       (growthRates['herbivores'][0])*num_herbivores + (parameterMatrix['herbivores']['herbivores']*num_herbivores*num_herbivores)
        + (parameterMatrix['herbivores']['youngScrub']*num_herbivores*num_youngScrub)
        + (parameterMatrix['herbivores']['matureScrub']*num_herbivores*num_matureScrub)
        + (parameterMatrix['herbivores']['sapling']*num_herbivores*num_sapling)
        + (parameterMatrix['herbivores']['matureTree']*num_herbivores*num_matureTree)
        + (parameterMatrix['herbivores']['grassHerbs']*num_herbivores*num_grassHerb)
        ,

        # young scrub (X1)
          (growthRates['youngScrub'][0])*num_youngScrub + (parameterMatrix['youngScrub']['herbivores']*num_youngScrub*num_herbivores)
        + (parameterMatrix['youngScrub']['youngScrub']*num_youngScrub*num_youngScrub)
        + (parameterMatrix['youngScrub']['matureScrub']*num_youngScrub*num_matureScrub)
        + (parameterMatrix['youngScrub']['sapling']*num_youngScrub*num_sapling)
        + (parameterMatrix['youngScrub']['matureTree']*num_youngScrub*num_matureTree)
        + (parameterMatrix['youngScrub']['grassHerbs']*num_youngScrub*num_grassHerb)
        ,

        # mature scrub
          (growthRates['matureScrub'][0])*num_matureScrub + (parameterMatrix['matureScrub']['herbivores']*num_matureScrub*num_herbivores)
        + (parameterMatrix['matureScrub']['youngScrub']*num_matureScrub*num_youngScrub)
        + (parameterMatrix['matureScrub']['matureScrub']*num_matureScrub*num_matureScrub)
        + (parameterMatrix['matureScrub']['sapling']*num_matureScrub*num_sapling)
        + (parameterMatrix['matureScrub']['matureTree']*num_matureScrub*num_matureTree)
        + (parameterMatrix['matureScrub']['grassHerbs']*num_matureScrub*num_grassHerb)
        ,

        # saplings 
          (growthRates['sapling'][0])*num_sapling + (parameterMatrix['sapling']['herbivores']*num_sapling*num_herbivores)
        + (parameterMatrix['sapling']['youngScrub']*num_sapling*num_youngScrub)
        + (parameterMatrix['sapling']['matureScrub']*num_sapling*num_matureScrub)
        + (parameterMatrix['sapling']['sapling']*num_sapling*num_sapling)
        + (parameterMatrix['sapling']['matureTree']*num_sapling*num_matureTree)
        + (parameterMatrix['sapling']['grassHerbs']*num_sapling*num_grassHerb)
        ,

     # mature trees 
          (growthRates['matureTree'][0])*num_matureTree + (parameterMatrix['matureTree']['herbivores']*num_matureTree*num_herbivores)
        + (parameterMatrix['matureTree']['youngScrub']*num_matureTree*num_youngScrub)
        + (parameterMatrix['matureTree']['matureScrub']*num_matureTree*num_matureScrub)
        + (parameterMatrix['matureTree']['sapling']*num_matureTree*num_sapling)
        + (parameterMatrix['matureTree']['matureTree']*num_matureTree*num_matureTree)
        + (parameterMatrix['matureTree']['grassHerbs']*num_matureTree*num_grassHerb)
        ,

        # grass & herbaceous plants
        (growthRates['grassHerbs'][0])*num_grassHerb + (parameterMatrix['grassHerbs']['herbivores']*num_grassHerb*num_herbivores)
        + (parameterMatrix['grassHerbs']['youngScrub']*num_grassHerb*num_youngScrub)
        + (parameterMatrix['grassHerbs']['matureScrub']*num_grassHerb*num_matureScrub)
        + (parameterMatrix['grassHerbs']['sapling']*num_grassHerb*num_sapling)
        + (parameterMatrix['grassHerbs']['matureTree']*num_grassHerb*num_matureTree)
        + (parameterMatrix['grassHerbs']['grassHerbs']*num_grassHerb*num_grassHerb)
                  ])


# time points 
t = np.linspace(0, 50)             
# Initial conditions
X0=np.empty(6)
X0[0]= 10
X0[1] = 30
X0[2] = 50
X0[3] = 70
X0[4] = 90
X0[5] = 110

X0 = np.array([X0[0], X0[1], X0[2], X0[3], X0[4], X0[5]])

# Integrate the ODEs
X = integrate.odeint(ecoNetwork, X0, t)


Is this possible and if so, what's the best way to do it?这可能吗?如果可以,最好的方法是什么?

Start by storing your species in a list:首先将您的物种存储在列表中:

species = ["herbivores", "youngScrub", "matureScrub", "sapling", "matureTree", "grassHerbs"]

Then you can loop over this list instead of manually typing out each one:然后,您可以遍历此列表,而不是手动输入每个列表:

new_array = []

for outer_index, outer_animal in enumerate(species):

    new_sum = (growthRates[outer_animal][0])* X[outer_index]

    for inner_index, inner_animal in enumerate(species):

        new_sum += np.sum(parameterMatrix[outer_animal][inner_animal]*X[outer_index]*X[inner_index])

    new_array.append(new_sum)

Further out, I'd encourage you to check out pandas , it's a great python library to work with dataframes.更进一步,我鼓励您查看pandas ,这是一个很棒的 python 库,可用于处理数据帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM