python - looping into different positions of a list -
i loop list of pandas dataframe variables, possibility of choosing different index positions
raw_data = {'v1': [10,np.nan,50,30],'v2': [10,-10,50,-20],'v3':[120,-130,509,-230],'v4': [5,78,34,66]} df = pd.dataframe(raw_data, columns = ['v1','v2','v3','v4']) columns = ['v1','v2','v3','v4'] #pseudo code: i+1 represents following element in list, after "i" element in columns: print(df[i]) print(df[i+1])
in real life more complex operations pairs of variables, applying function v1 , v3, v2 , v4, v3 , v5, etc.
for instance: create new var called 'new_varv1, difference between v1 , v3. loop same v2 , v4, storing result in new_varv2, etc
for in columns: df['new_var'+i]=df[i]-df[i+2]
if understanding question correctly, this:
ctr = 1 in columns: print(df[i]) print(df[columns[ctr]]) ctr+=1
Comments
Post a Comment