熊猫-Groupby或将多个数据帧切成垃圾箱

时间:2020-03-02 16:21:30

标签: python pandas dataframe matplotlib data-visualization

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

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

我正在绘制一个热图。我需要该地图的每个“区域”都有一条线,该线显示距该区域具有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

1 个答案:

答案 0 :(得分:2)

像这样吗?

(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