在嵌套循环中附加数据框 [英] append dataframe in nested loop
问题描述
我有以下代码根据 2 个变量(bh 和频率)在函数中进行一些计算.当我以固定值 bh 运行不带循环的代码时,我会得到一个正确的输出和数据帧,保存在 .csv 文件中.:
I have the following code that does some calculation in a function depending on 2 variables (bh and frequency). when i run the code without loop with a fixed value of bh i get a proper output and data frame saved in a .csv file.:
bh frequency Re Im
0 1e-05 1 5.86848609615851 -0.999374346845734
1 1e-05 11 4.34298196390882 -0.994875549720418
2 1e-05 21 3.93236459069042 -0.99112086235206
3 1e-05 31 3.68545733552675 -0.987695572513367
4 1e-05 41 3.50849758486341 -0.984487932588323
但是,当我在 bh 上的循环中编码时,我想在 bh 值和频率列表上循环,我得到与之前相同的输出,这意味着它没有循环.任何人都有解决方案来修改数据帧或将 eaxh bh 循环输出保存在新的 .csv 中,以便稍后绘制数据.
however, i would like to loop on a list of bh values and frequency when i code in a loop on bh i get the same output as before meaning it doesn't loop. would anyone has a solution to amend the dataframe or save eaxh bh loop output in a new .csv in order to plot the data later on.
from mpmath import *
import numpy as np
import cmath
import math
import pandas as pd
mp.dps = 15; mp.pretty = True
a = mpf(0.25)
b = mpf(0.25)
z = mpf(0.75)
frequency = np.arange(1, 50, 10) # frequency range
bh = np.arange(10e-6, 30e-6, 10e-6) #10e-6 # width
print(bh)
D = 1e-6 #7.8e-4 # diffusivity
gamma = 0.5772 # Euler constant
v = []
w =[]
i = []
def q(frequency):
for i in bh:
# for f in frequency:
omega = (((i ** 2) * 2 * math.pi * frequency) / D) # depends on bh and frequency
u = ((-j/(math.pi * omega))*meijerg([[1, 3/2], []], [[1, 1], [0.5, 0]], j*omega))
v = np.real(u)
w = np.imag(u)
return i, frequency, v, w
#transpose arrays
T = np.vectorize(q)
print(T(frequency))
df = np.array(T(frequency)).T
print(df)
# create DataFrame
df1 = pd.DataFrame(data=df, columns=['bh', 'frequency','Re', 'Im'])
print(df1)
#save in .csv
df1.to_csv('C:\\Users\\calculations\\T.csv')
推荐答案
我不完全确定我是否理解您的问题域,但您似乎正在返回循环的第一次迭代.通过在循环内使用 return,您实际上可以提前终止 for 循环.这应该在循环之外.此外,您不会将值保存在数组 v、w 和 i 中.您正在覆盖变量.
I am not entirely sure if I understand your problem domain, but you seem to be returning the first iteration of the loop. By having return inside the loop you essentially terminate the for loop early. This should be outside of the loop. Also, you do not save your values in the arrays v, w, and i. You are overwriting the variable.
我做了一些修改(根据您的问题域可能不正确),但它应该可以完成您想要完成的工作.
I have done some modifications (maybe not correct according to your problem domain), but it should do what you want to accomplish.
from mpmath import *
import numpy as np
import cmath
import math
import pandas as pd
mp.dps = 15; mp.pretty = True
a = mpf(0.25)
b = mpf(0.25)
z = mpf(0.75)
frequencies = np.arange(1, 50, 10) # frequency range
bh = np.arange(10e-6, 30e-6, 10e-6) #10e-6 # width
print(bh)
D = 1e-6 #7.8e-4 # diffusivity
gamma = 0.5772 # Euler constant
v = []
w = []
i = []
bhs = []
freqs = []
def q(frequencies):
for frequency in frequencies:
for i in bh:
# for f in frequency:
omega = (((i ** 2) * 2 * math.pi * frequency) / D) # depends on bh and frequency
u = ((-j/(math.pi * omega))*meijerg([[1, 3/2], []], [[1, 1], [0.5, 0]], j*omega))
v.append(np.real(u))
w.append(np.imag(u))
bhs.append(i)
freqs.append(frequency)
return bhs, freqs, v, w
data = np.array(q(frequencies)).T
# create DataFrame
df1 = pd.DataFrame(data=data, columns=['bh', 'frequency','Re', 'Im'])
df1
输出:
bh frequency Re Im
0 1e-05 1 5.86848609615851 -0.999374346845734
1 2e-05 1 4.98625732244539 -0.99786698700645
2 1e-05 11 4.34298196390882 -0.994875549720418
3 2e-05 11 3.46384911041305 -0.983559190454865
4 1e-05 21 3.93236459069042 -0.99112086235206
5 2e-05 21 3.05626898509369 -0.972212391507732
6 1e-05 31 3.68545733552675 -0.987695572513367
7 2e-05 31 2.81234917403506 -0.962167989599812
8 1e-05 41 3.50849758486341 -0.984487932588323
9 2e-05 41 2.63833200578647 -0.952979213441469
10 1e-05 1 5.86848609615851 -0.999374346845734
11 2e-05 1 4.98625732244539 -0.99786698700645
12 1e-05 11 4.34298196390882 -0.994875549720418
13 2e-05 11 3.46384911041305 -0.983559190454865
14 1e-05 21 3.93236459069042 -0.99112086235206
15 2e-05 21 3.05626898509369 -0.972212391507732
16 1e-05 31 3.68545733552675 -0.987695572513367
17 2e-05 31 2.81234917403506 -0.962167989599812
18 1e-05 41 3.50849758486341 -0.984487932588323
19 2e-05 41 2.63833200578647 -0.952979213441469
20 1e-05 1 5.86848609615851 -0.999374346845734
21 2e-05 1 4.98625732244539 -0.99786698700645
22 1e-05 11 4.34298196390882 -0.994875549720418
23 2e-05 11 3.46384911041305 -0.983559190454865
24 1e-05 21 3.93236459069042 -0.99112086235206
25 2e-05 21 3.05626898509369 -0.972212391507732
26 1e-05 31 3.68545733552675 -0.987695572513367
27 2e-05 31 2.81234917403506 -0.962167989599812
28 1e-05 41 3.50849758486341 -0.984487932588323
29 2e-05 41 2.63833200578647 -0.952979213441469
这篇关于在嵌套循环中附加数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!