gpt4 book ai didi

python - 在 Python 中循环遍历不同数据集的行和列

转载 作者:行者123 更新时间:2023-12-04 10:15:45 28 4
gpt4 key购买 nike

我是 Python 新手,正在努力遍历两个不同数据集的行和列,以生成一组值。

我有两个数据框(parameterMatrix 和 growthRates);一个显示了一系列物种及其相互作用的强度,另一个显示了每个物种的增长率。

参数矩阵 :

             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

增长率 :
herbivores  youngScrub  matureScrub  sapling  matureTree  grassHerbs
0 0.1 0 0.1 0.1 0 0.2

我正在尝试为六个物种中的每一个生成一组值,最终可用于计算每个物种随时间的变化率。我已经手动写出每个方程(见下文),但我想知道是否有更快的方法来做到这一点,例如通过循环遍历这些数据帧中的每一个。
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)



这可能吗,如果可能,最好的方法是什么?

最佳答案

首先将您的物种存储在列表中:

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

然后你可以遍历这个列表,而不是手动输入每一个:
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)

更进一步,我鼓励您查看 pandas ,这是一个很好的 Python 库,可以处理数据帧。

关于python - 在 Python 中循环遍历不同数据集的行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61065013/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com