gpt4 book ai didi

python - Pandas - Groupby 或将多个数据帧剪切到垃圾箱

转载 作者:行者123 更新时间:2023-12-02 00:47:39 25 4
gpt4 key购买 nike

我有一个带有起点轴点和终点的数据框

x       y       x_end   y_end   distance
14.14 30.450 31.71 41.265 20.631750
-27.02 55.650 -33.60 63.000 9.865034
-19.25 70.665 -28.98 80.115 13.563753
16.45 59.115 9.94 41.895 18.409468

我正在绘制热图。我需要该 map 的每个“区域”都有一条线,显示从该区域具有 x/y 的线的平均距离和角度及其 x_end/y_end。看起来像这样
enter image description here

我的垃圾箱是
xbins = np.linspace(-35, 35, 11)

ybins = np.linspace(0, 105, 22)

我已经绘制了热图

enter image description here

我正在寻找这样的东西
Bins_X           Bins_Y     Average_X_End   Average_Y_End   Average_Distance
(-35.0, -28.0] (0, 5.0] 31.71 41.265 20.631750
(-28.0, -21.0] (0, 5.0] -33.60 63.000 9.865034
(-21.0, -14.0] (0, 5.0] -28.98 80.115 13.563753
(-14.0, -7.0] (0, 5.0] 9.94 41.895 18.409468
(-35.0, -28.0] (5.0, 10.0] 41.265 31.71 13.563753
(-28.0, -21.0] (5.0, 10.0] 63.000 -33.60 18.409468
(-21.0, -14.0] (5.0, 10.0] 80.115 -28.98 20.631750
(-14.0, -7.0] (5.0, 10.0] 41.895 9.94 9.865034

最佳答案

像这样的东西?

(df.drop(['x','y'], axis=1)
.groupby([pd.cut(df.x, xbins),
pd.cut(df.y, ybins)],
)
.mean()
.dropna(how='all')
.add_prefix('Average_')
)

输出:
                             Average_x_end  Average_y_end  Average_distance
x y
(-28.0, -21.0] (55.0, 60.0] -33.60 63.000 9.865034
(-21.0, -14.0] (70.0, 75.0] -28.98 80.115 13.563753
(14.0, 21.0] (30.0, 35.0] 31.71 41.265 20.631750
(55.0, 60.0] 9.94 41.895 18.409468

关于python - Pandas - Groupby 或将多个数据帧剪切到垃圾箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60492857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com