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

标签 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

我正在绘制热图。我需要该 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/

相关文章:

python - 如何在 Python 中将子集行位置重置为行位置 0

python - DataFrame 列的所有可能组合 - pandas/python

Python 和 C 耦合

Python ctype 直接调用具有原始偏移量的函数

python - 使用 iterrows(),从数据帧的单行中插入列的子集作为另一个数据帧中的新行

python - 根据 pandas dataframe 的其他列的值更改一列的值

R 使用先前的迭代值对 FOR 循环进行矢量化

python - Python 中用户定义的 croston 函数

python - 如何在字符串中定义过滤器或表达式?

python - 逗号分隔值文件的列值中的逗号 - python 读取问题