python - for loop for calculations from dataframes -


so, have text file have turned dataframe. i'm trying loop on every value column h , z, square each of them , take square root (pythagoras' theorem in other words). i.e.

f = sqrt(h**2 + z**2). 

my data:(after been turned pandas dataframe)

    h        d       z 0   3235     6764    9546 1   1667     3455    7776 2   3555     3564    5433 3   2344     3333    8777 4   5666     3334    4444 

that's tiny snippet of data. there 1000> rows of data here.

code:

import pandas pd  #load data:  df=pd.read_table('example_data.txt', sep='\s+')  math import sqrt x,y in df:     f=sqrt(h**2+z**2)     print(f) 

the error produced:

valueerror                                traceback (most recent call last) <ipython-input-34-1b1be5be91d0> in <module>()         1 math import sqrt   ----> 2 x,y in df:         3     f=sqrt(h**2+z**2)         4     print(f)         5           valueerror: many values unpack (expected 2) 

so that's it. easy question feel, 1 python beginner can't seem solve. repeating calculation many times on 2 variables. i've seen 1 variable not 2.

any appreciated,

cheers!

you should use numpy vectorise this:

in[11]: df['f'] = np.sqrt(df['h']**2 +  df['z']**2) df  out[11]:        h     d     z             f 0  3235  6764  9546  10079.252998 1  1667  3455  7776   7952.676593 2  3555  3564  5433   6492.727778 3  2344  3333  8777   9084.605935 4  5666  3334  4444   7200.881335 

regarding error, iterable returned dataframe columns:

for col in df:     print(col)  h d z f 

hence error, iterate on rows use iterrows:

from math import sqrt x,y in df.iterrows():     f=sqrt(y['h']**2+y['z']**2)     print(f)  10079.252998114493 7952.676593449529 6492.72777806062 9084.605935317173 7200.881334947827 

but simple arithmetic operations should avoid loops , vectorised solutions such numpy faster looping , scale better when data size increases.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -