Home > Mobile >  For loop with non-consecutive indices
For loop with non-consecutive indices

Time:03-29

I'm quite new to Phyton and working with data frames, so this might be a very simple problem.

I successfully imported some measurement data (1 minute resolution) and did some calculations on them. I want to recalculate some data processing on a 15 minute basis (not average), for which I extracted every row at :00, :15, :30 and :45 from the original data frame.

df_interval = df[(df['DateTime'].dt.minute == 0) | (df['DateTime'].dt.minute == 15) | (df['DateTime'].dt.minute == 30) | (df['DateTime'].dt.minute == 45)]

This seems to work fine. Now I want to recalculate the concentration every 15 minute based on what the instrument is internally doing, which is a simple formula.

So what I tried is:

for i in df_interval.index:
    if np.isnan(df_interval.ATN[i]) == False and np.isnan(df_interval.ATN[i 1]) == False:  
            df_15min = (0.785 *((df_interval.ATN[i 1]-df_interval.ATN[i])/100))/(df_interval.Flow[i]*(1-0.07)*10.8*(1-df_interval.K[i]*df_interval.ATN[i])*15)

however, I end up with a KeyError: 226. And I don't understand why...

Update:

Here is the data and in the last column (df_15min) also the result that I want to get:

ATN Flow K df_15min
150 3647 0.00994
165 3634 0.00996
180 3634 0.00995
195 3621 0.00995
210 3615 0.00994
225 1.703678939 3754 0.00994 3.75E-08
240 4.356519267 3741 0.00994 3.84E-08
255 6.997422571 3741 0.00994 3.94E-08
270 9.627710046 3736 0.00995 4.02E-08
285 12.23379251 3728 0.01007 3.89E-08
300 14.67175418 3727 0.01026 3.76E-08
315 16.9583747 3714 0.01043 3.73E-08
330 19.1497249 3714 0.01061 3.96E-08
345 21.39628083 3709 0.01079 3.87E-08
360 23.51512717 3701 0.01086 4.02E-08
375 25.63995721 3700 0.01083 3.90E-08
390 27.63886191 3688 0.0108 3.47E-08
405 29.36343728 3688 0.01076 3.68E-08
420 31.14291069 3677 0.01072 3.90E-08

I do a lot of things in Igor, so that is how I would do it there (unfortunately for me, it has to be in python this time):

variable i
    For (i=0; i<numpnts(ATN)-1; i =1)
        df_15min[i] = (0.785 *((ATN[i 1]-ATN[i])/100))/(Flow[i]*(1-0.07)*10.8*(1-K[i]*ATN[i])*15)
    endfor

Any help would be appreciated, thanks!

CodePudding user response:

You can literally write the same operation as vectorial code. Just use the whole rows and shift(-1) to get the "next" row.

df['df_15min'] = (0.785 *((df['ATN'].shift(-1)-df['ATN'])/100))/(df['Flow']*(1-0.07)*10.8*(1-df['K']*df['ATN'])*15)

Or using diff:

df['df_15min'] = (0.785 *((-df['ATN'].diff(-1))/100))/(df['Flow']*(1-0.07)*10.8*(1-df['K']*df['ATN'])*15)

output:

             ATN  Flow        K      df_15min
index                                        
150          NaN  3647  0.00994           NaN
165          NaN  3634  0.00996           NaN
180          NaN  3634  0.00995           NaN
195          NaN  3621  0.00995           NaN
210          NaN  3615  0.00994           NaN
225     1.703679  3754  0.00994  3.745468e-08
240     4.356519  3741  0.00994  3.844700e-08
255     6.997423  3741  0.00994  3.937279e-08
270     9.627710  3736  0.00995  4.019633e-08
285    12.233793  3728  0.01007  3.886148e-08
300    14.671754  3727  0.01026  3.763219e-08
315    16.958375  3714  0.01043  3.734876e-08
330    19.149725  3714  0.01061  3.955360e-08
345    21.396281  3709  0.01079  3.870011e-08
360    23.515127  3701  0.01086  4.017342e-08
375    25.639957  3700  0.01083  3.897022e-08
390    27.638862  3688  0.01080  3.473242e-08
405    29.363437  3688  0.01076  3.675232e-08
420    31.142911  3677  0.01072           NaN

CodePudding user response:

Your if condition checks bc_interval.row1[i 1] for nan and then you access df_interval.row1[i 1]. Looks like you wanted to check df_interval.row1[i 1] instead.

  • Related