形成指数循环

2024-03-29 10:39:39 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在pandas(索引列的位置索引)中使用for循环执行以下类型的进程

z1=数学sqrt((df.loc[0,“A#1”]-A#u均值[0])**2+(df.loc[0,“B#1”]-B#u均值[0])**2)

z2=数学sqrt((df.loc[“A#2”]-A#u均值[0])**2+(df.loc[0,“B#2”]-B#u均值[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     ?               ?

Tags: 类型pandasdfforinput进程数学sqrt
1条回答
网友
1楼 · 发布于 2024-03-29 10:39:39

你是这个意思吗

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)

另一种可能性是使用dataframe.apply,不过如果有许多函数要应用于每一列,则可能需要多次调用

相关问题 更多 >