Python 形成指数循环
如何在索引列的位置索引时使用for循环执行以下类型的过程 z1=math.sqrt df.loc[0,a1]-A_均值[0]**2+df.loc[0,b1]-B_均值[0]**2Python 形成指数循环,python,pandas,numpy,Python,Pandas,Numpy,如何在索引列的位置索引时使用for循环执行以下类型的过程 z1=math.sqrt df.loc[0,a1]-A_均值[0]**2+df.loc[0,b1]-B_均值[0]**2 input: a_mean = [ 1, 2, 3] b_mean = [1, 2, 3 ] x#1 A #1 A #2 A #3 B#1 B#2 B#3 0 1 2 3 4 5 6
input:
a_mean = [ 1, 2, 3]
b_mean = [1, 2, 3 ]
x#1 A #1 A #2 A #3 B#1 B#2 B#3
0 1 2 3 4 5 6 7
1 1 2 3 4 5 6 7
output:
(z list [1] (z list [2)
z1 0 answers answers
z2 1 ? ?
z2=math.sqrt df.loc[a2]-A_均值[0]**2+df.loc[0,b2]-B_均值[0]**2
input:
a_mean = [ 1, 2, 3]
b_mean = [1, 2, 3 ]
x#1 A #1 A #2 A #3 B#1 B#2 B#3
0 1 2 3 4 5 6 7
1 1 2 3 4 5 6 7
output:
(z list [1] (z list [2)
z1 0 answers answers
z2 1 ? ?
这就是你的意思吗
z = []
for i in range(1,n):
z_p = []
for s in range(0,3):
z_p.append(math.sqrt( (df.loc["A #"+str(i)] - a_mean[s])**2 + (df.loc[s,"B #"+str(i)] - a_mean[s]) **2))
z.append(z_p)
另一种可能是使用,但如果有很多函数要应用于每列,则可能需要多次调用。是否介意编写df、a_平均值和预期输出?能否提供更详细的示例?可能只需要df['z']=math.sqrtdf['a']-a_平均值[0]**2+df['B']-a_平均值[0]**2谢谢你回答我的问题。现在我在想,我怎样才能添加到列表列表中,使每个都像组z=[[z1,z1,z1],[z2,z2,z2]]z=[]中的i在范围1中,n:对于范围3内的s:z.appendmath.sqrt df.loc[s,A+stri]-A_平均值[s]**2+df.loc[s,B+stri]-A_平均值[s]**2