mercredi 31 mai 2017

Loop nesting issue - values not correct

I have a dataframe that has two relevant columns (actually has >2, but don't think that's important), and one of the columns has duplicates in it.

The duplicates are in the column, HAB_slice['Radial Position'], and are in increments of 0.1.

Ideally, I want to say if two values in HAB_slice['Radial Position'] are equal to each other, find the absolute value difference between them and add them to a running total.

The current code looks like this:

    possible_pos = np.linspace(0, 1, 1 / stepsize+1) 
    center_sum = 0
    for i in range(0, len(possible_pos)): 
        temp = HAB_slice[HAB_slice['Radial Position']==possible_pos[i]]
        if len(temp) == 2:
            center_sum += np.abs(temp['I'].diff().values[1])
    print center_sum

And while it does return a value and doesn't throw errors, the value for center_sum is different than when I manually calculate it. I think it's just something wrong with the nesting but I'm pretty new to loops and am not really sure.

Example of the error: the following data yields a center_sum = 0 in this code, but if you manually calculate the absolute value differences in I when the Radial positions are equal to each other, it equals 0.0045878.

I           Radial Position
0.14289522  1
0.14298554  0.9
0.1430356   0.8
0.1430454   0.7
0.1430552   0.6
0.14266456  0.5
0.14227392  0.4
0.14234106  0.3
0.14286598  0.2
0.1433909   0.1
0.14309062  0
0.14279034  0.1
0.14271344  0.2
0.14285992  0.3
0.1430064   0.4
0.14327248  0.5
0.14353856  0.6
0.14356664  0.7
0.14335672  0.8
0.1431468   0.9
0.14338368  1

Aucun commentaire:

Enregistrer un commentaire