I'm trying to calculate the average location of an animal in a certain area.
I have this kind of dataframe, where each 'N° Tag' is the specific individual, Lat is the latitude of the recorded position and Lon is the longitude of the recorded position:
N° Tag Lat Lon
1 49.05567 -67.05242
4 49.05517 -67.05249
1 49.05575 -67.05247
2 49.05584 -67.05288
4 49.05523 -67.04214
2 49.05698 -67.05299
1 49.05567 -67.05246
1 49.05587 -67.05248
4 49.05477 -67.05211
I would calculate the median position (median Lat and Lon) of each animal and add a column in the present dataframe with such value like this :
N° Tag Lat Lon Median Lat Median Lon
1 49.05567 -67.05242 49.05562 67.05562
4 49.05517 -67.05249 49.05612 67.05515
1 49.05575 -67.05247 49.05562 67.05562
2 49.05584 -67.05288 49.05571 67.05526
4 49.05523 -67.04214 49.05612 67.05515
2 49.05698 -67.05299 49.05571 67.05526
1 49.05567 -67.05246 49.05562 67.05562
1 49.05587 -67.05248 49.05562 67.05562
4 49.05477 -67.05211 49.05612 67.05515
Thanks for the help!
CodePudding user response:
Try using transform()
:
df['median lon'] = df['lon'].groupby(df['n tag']).transform('median')
CodePudding user response:
How about
df['Median Lon'] = df['Lon'].groupby(df['Lon']).transform('median')
df['Median Lat'] = df['Lat'].groupby(df['Lat']).transform('median')
df
Out[86]:
N°Tag Lat Lon Median Lon Median Lat
0 1 49.05567 -67.05242 -67.05242 49.05567
1 4 49.05517 -67.05249 -67.05249 49.05517
2 1 49.05575 -67.05247 -67.05247 49.05575
3 2 49.05584 -67.05288 -67.05288 49.05584
4 4 49.05523 -67.04214 -67.04214 49.05523
5 2 49.05698 -67.05299 -67.05299 49.05698
6 1 49.05567 -67.05246 -67.05246 49.05567
7 1 49.05587 -67.05248 -67.05248 49.05587
8 4 49.05477 -67.05211 -67.05211 49.05477