Reputation: 33
How to I do the following type of process using a for
loop in pandas
(indexing the the location index of the column)
z1 = math.sqrt( (df.loc[0,"A #1"] - a_mean[0])**2 + (df.loc[0,"B #1"] - b_mean[0]) **2)
z2 = math.sqrt( (df.loc["A #2"] - a_mean[0])**2 + (df.loc[0,"B #2"] - b_mean[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 ? ?
Upvotes: 0
Views: 47
Reputation: 764
Is this what you mean?
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)
Another possibility is to use dataframe.apply, though you might need multiple calls if you have a lot of functions to apply to each column.
Upvotes: 1