简体   繁体   中英

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.

I have two dataframes (parameterMatrix and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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