熊猫按某些列分组

时间:2019-03-17 14:48:50

标签: python pandas dataframe pandas-groupby

说明

如何使用Pandas groupby对某些列进行分组,而不对其他列进行分组?

当前进度

table_D = pd.DataFrame({
    'Geo_ID': [1, 1, 1, 1, 2, 3, 4, 4, 5],
    'A_Code': [12, 12, 12, 65, 65, 65, 65, 98, 98],
    'A_Cost': [2, 9, 1, 10, 6, 7, 7, 6, 2],
}, columns=['Geo_ID', 'A_Code', 'A_Cost'])
table_D_dummies = pd.get_dummies(data = table_D, columns = ["A_Code"])
table_D_dummies_grouped = table_D_dummies.groupby(by = ["Geo_ID"]).sum()

问题

如下所示,这可以按Geo_ID正确计算费用。不幸的是,它也通过A_Code求和。

A_Code_12,A_Code_65和A_Code_98应该分别组合 。此外,在实际数据集中,有超过100个A_Code。

数据

table_D

+--------+--------+--------+
| Geo_ID | A_Code | A_Cost |
+--------+--------+--------+
|      1 |     12 |      2 |
|      1 |     12 |      9 |
|      1 |     12 |      1 |
|      1 |     65 |     10 |
|      2 |     65 |      6 |
|      3 |     65 |      7 |
|      4 |     65 |      7 |
|      4 |     98 |      6 |
|      5 |     98 |      2 |
+--------+--------+--------+

table_D_dummies

+---+--------+--------+-----------+-----------+-----------+
|   | Geo_ID | A_Cost | A_Code_12 | A_Code_65 | A_Code_98 |
+---+--------+--------+-----------+-----------+-----------+
| 0 |      1 |      2 |         1 |         0 |         0 |
| 1 |      1 |      9 |         1 |         0 |         0 |
| 2 |      1 |      1 |         1 |         0 |         0 |
| 3 |      1 |     10 |         0 |         1 |         0 |
| 4 |      2 |      6 |         0 |         1 |         0 |
| 5 |      3 |      7 |         0 |         1 |         0 |
| 6 |      4 |      7 |         0 |         1 |         0 |
| 7 |      4 |      6 |         0 |         0 |         1 |
| 8 |      5 |      2 |         0 |         0 |         1 |
+---+--------+--------+-----------+-----------+-----------+

table_D_dummies_grouped

+--------+--------+-----------+-----------+-----------+
| Geo_ID | A_Cost | A_Code_12 | A_Code_65 | A_Code_98 |
+--------+--------+-----------+-----------+-----------+
|      1 |     22 |         3 |         1 |         0 |
|      2 |      6 |         0 |         1 |         0 |
|      3 |      7 |         0 |         1 |         0 |
|      4 |     13 |         0 |         1 |         1 |
|      5 |      2 |         0 |         0 |         1 |
+--------+--------+-----------+-----------+-----------+

1 个答案:

答案 0 :(得分:2)

您没有使用虚拟表,而是对原始数据框进行了分组:

table_D_dummies = pd.get_dummies(data = table_D, columns = ["A_Code"])
table_D_dummies_grouped = table_D.groupby(by = ["Geo_ID"]).sum()

您要在此处将table_D_dummies分组:

>>> table_D_dummies
   Geo_ID  A_Cost  A_Code_12  A_Code_65  A_Code_98
0       1       2          1          0          0
1       1       9          1          0          0
2       1       1          1          0          0
3       1      10          0          1          0
4       2       6          0          1          0
5       3       7          0          1          0
6       4       7          0          1          0
7       4       6          0          0          1
8       5       2          0          0          1
>>> table_D_dummies.groupby(by = ["Geo_ID"]).sum()
        A_Cost  A_Code_12  A_Code_65  A_Code_98
Geo_ID
1           22          3          1          0
2            6          0          1          0
3            7          0          1          0
4           13          0          1          1
5            2          0          0          1

如果您需要汇总每个假人的成本 ,请将这些成本添加到分组列中:

>>> table_D_dummies.groupby(by = [
...     "Geo_ID",
...     *(c for c in table_D_dummies.columns if c.startswith('A_Code_'))
... ]).sum()
                                      A_Cost
Geo_ID A_Code_12 A_Code_65 A_Code_98
1      0         1         0              10
       1         0         0              12
2      0         1         0               6
3      0         1         0               7
4      0         0         1               6
                 1         0               7
5      0         0         1               2