[英]How to calculate the median of different elements in a dataframe in python
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:我有这种dataframe,其中每个'N°Tag'都是特定的个体,Lat是记录的position的纬度,Lon是记录的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:我会计算每只动物的中位数 position(中位数 Lat 和 Lon),并在当前 dataframe 中添加一列,其值如下:
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!谢谢您的帮助!
Try using transform()
:尝试使用
transform()
:
df['median lon'] = df['lon'].groupby(df['n tag']).transform('median')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.