python - 对 python pandas 数据框进行装箱 : extracting bin centers and the sum of another column

标签 python pandas group-by binning

我在对 pandas 数据框进行分箱然后提取必要的绘图变量时遇到了一些麻烦。

我有一个如下所示的 pandas 数据框:

            a    ad    td  price  result  profit_loss  
12935   10809 -1181  2363    262     1.0     616743.0  
13025  -18771   696  1390    350     1.0       1390.0  
13079  -20154   348   695    305     0.0        695.0  
13085    2370  3945  3150    264     0.0    -828450.0 

我想将数据帧的行分箱到“td”字段的相同大小的箱中(例如td = 0-100、100-200、200-300),并计算属于的所有profit_loss条目的总和那个td bin

例如在这里,对于 0-2000 的 td bin,profit_loss 将为 1390+695。

然后我想绘制 td bin 中心与profit_loss 总和的图。

我已经尝试过:

bins = np.linspace(df.td.min(), df.td.max(), 10)
groups = df.groupby(pd.cut(df.td, bins))

但我不确定如何提取 td bin 中心和创建的profit_loss 总和并绘制它们。

谢谢!

最佳答案

如果您想在 X 轴上显示所有空箱 - 您可以这样做:

import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')

new = pd.DataFrame({'td':range(0, int(round(df.td.max() / 100) * 100) + 100, 100)})

(pd.merge(new, df.groupby(df.td//100*100)['profit_loss']
                 .sum().reset_index(),
          how='left')
   .fillna(0)
   .set_index('td')
   .plot.bar()
)
plt.axhline(0, color='k')

enter image description here

说明:

一个辅助 DF,包含所有 bin

In [68]: new
Out[68]:
      td
0      0
1    100
2    200
3    300
4    400
5    500
6    600
7    700
8    800
9    900
10  1000
11  1100
12  1200
13  1300
14  1400
15  1500
16  1600
17  1700
18  1800
19  1900
20  2000
21  2100
22  2200
23  2300
24  2400
25  2500
26  2600
27  2700
28  2800
29  2900
30  3000
31  3100
32  3200

分组原始DF

In [71]: df.groupby(df.td//100*100)['profit_loss'].sum().reset_index()
Out[71]:
     td  profit_loss
0   600        695.0
1  1300       1390.0
2  2300     616743.0
3  3100    -828450.0

合并/生成的 DF

In [69]: (pd.merge(new, df.groupby(df.td//100*100)['profit_loss']
   ....:                  .sum().reset_index(),
   ....:           how='left')
   ....:    .fillna(0)
   ....: )
Out[69]:
      td  profit_loss
0      0          0.0
1    100          0.0
2    200          0.0
3    300          0.0
4    400          0.0
5    500          0.0
6    600        695.0
7    700          0.0
8    800          0.0
9    900          0.0
10  1000          0.0
11  1100          0.0
12  1200          0.0
13  1300       1390.0
14  1400          0.0
15  1500          0.0
16  1600          0.0
17  1700          0.0
18  1800          0.0
19  1900          0.0
20  2000          0.0
21  2100          0.0
22  2200          0.0
23  2300     616743.0
24  2400          0.0
25  2500          0.0
26  2600          0.0
27  2700          0.0
28  2800          0.0
29  2900          0.0
30  3000          0.0
31  3100    -828450.0
32  3200          0.0

关于python - 对 python pandas 数据框进行装箱 : extracting bin centers and the sum of another column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37838431/

相关文章:

python - 矢量化(手动)正向替换

php - 选择一列的最大值,按另一列分组不起作用

mysql - 如何使用连接在不使用 max 函数的情况下显示每个部门的最高工资

SQL Server 2018如何将信息分组到不同的列中

python - 为什么 Python 描述符适用于类级别属性而不适用于实例级别属性

python - 将 JS 变量发布到 Django View 并在单独的模板中显示为上下文变量

python - 如何在python中选择一行中最小的数字?

pandas - 为什么 pandas.melt 会弄乱我的数据类型?

python - 如何使用按值分组创建新的 pandas DataFrame?

python-3.x - 导入 Pandas 显示 ImportError : cannot import name hashtable