pandas dataframe: groupby, apply two conditions and go back to initial dataframe

Viewed 57

I have a panda dataframe df, representing two groups of points identified by their id, their physical coordinates (x, y), R the distance of each point from the center of the coordinates (x_c,y_c), and TR is a threshold.

        id      x     y          R         TR       x_c     y_c
1  256780.0  14135  6328  85.114465  20.986341  14050.83  6315.33
2  256780.0  14033  6323  19.411480  20.986341  14050.83  6315.33
3  256780.0  14024  6343  38.541824  20.986341  14050.83  6315.33
4  256780.0  14111  6379  87.598357  20.986341  14050.83  6315.33
5  256780.0  14013  6278  53.152036  20.986341  14050.83  6315.33
6  256780.0  13989  6241  96.689222  20.986341  14050.83  6315.33
7    2000.0  14060  6287   1.000000   0.000000  14060.0   6288.00 
8    2000.0  14060  6289   1.000000   0.000000  14060.0   6288.0

I want to do the following steps: groupby df imposing different conditions on R (if the condition is satisfied, compute new (x_c,y_c), if not leave the initial (x_c,y_c) values), and then map back the results on the original dataframe df.

I write something, but it does not work well. First, impose two conditions:

condition_1 = df['R'] < 3*df['TR']
condition_2 = df['R'] > 3*df['TR']

if condition_1 is satisfied, I want to compute again (x_c,y_c) in this way:

new_center=df[cond1].groupby('id', as_index=False)['x','y'].mean()

if condition_1 is not satified (thus, condition_2 is), I want that new_center is still equal to the old (x_c,y_c), something similar to:

new_center=df.groupby('id').['x_c','y_c']

Finally, I want to map back the new_center on df.

In terms of dataframe, I want df to be like this:

        id      x     y          R         TR       x_c     y_c
1  256780.0  14135  6328  85.114465  20.986341   14033.0  6323.0  new values
2  256780.0  14033  6323  19.411480  20.986341   14033.0  6323.0
3  256780.0  14024  6343  38.541824  20.986341   14033.0  6323.0
4  256780.0  14111  6379  87.598357  20.986341   14033.0  6323.0
5  256780.0  14013  6278  53.152036  20.986341   14033.0  6323.0
6  256780.0  13989  6241  96.689222  20.986341   14033.0  6323.0
7    2000.0  14060  6287   1.000000   0.000000   14060.0  6288.0  old values
8    2000.0  14060  6289   1.000000   0.000000   14060.0  6288.0

starting dataframe df.to_dict()

{'R': {0: 1.0,  1: 1.0,  2: 85.114465,  3: 19.411480,  4: 38.541824,  5: 87.598357,  6: 53.152036,  7: 96.68922},
'id': {0: 2000.0,  1: 2000.0,  2: 256780.0,  3: 256780.0,  4: 56780.0,  5: 256780.0,  6: 256780.0,  7: 256780.0}, 
 'TR': {0: 0.0,  1: 0.0,  2: 20.986341,  3: 20.986341,  4: 20.986341,  5: 20.986341,  6: 20.986341,  7: 20.986341},
'x': {0: 14060,  1: 14060,  2: 14135,  3: 14033,  4: 14024,  5: 14111,  6: 14013,  7: 13989},
'y': {0: 6287,  1: 6289,  2: 6328, 3: 6323, 4: 6343, 5: 6379, 6: 6278, 7: 6241},
'x_c': {0: 14060.0,  1: 14060.0,  2: 14050.83,  3: 14050.83,  4: 14050.83,  5: 14050.83,  6: 14050.83,  7: 14050.83},
'y_c': {0: 6288.0,  1: 6288.0,  2: 6315.33,  3: 6315.33,  4: 6315.33,  5: 6315.33,  6: 6315.33,  7: 6315.33}}

0 Answers
Related