Python groupby函数可计算2列数据

时间:2018-10-16 15:03:18

标签: python pandas-groupby

我的数据看起来像这样:

+------------+-----+--------+
|    Room    | TV  | Lights |
+------------+-----+--------+
| Bedroom1   | On  | Off    |
| Bedroom2   | On  | On     |
| LivingRoom | Off | On     |
| Bedroom2   | On  | On     |
| LivingRoom | Off | Off    |
| Bedroom1   | Off | On     |
| LivingRoom | On  | On     |
| Bedroom2   | On  | On     |
| Bedroom2   | On  | On     |
| Bedroom1   | On  | Off    |
| LivingRoom | On  | On     |
+------------+-----+--------+

我正在尝试使用pandas groupby函数来计算每个房间的开和关数量。因此输出将是这样的:

+------------+--------+---------+------------+-------------+
|            | TV(On) | TV(Off) | Lights(On) | Lights(Off) |
+------------+--------+---------+------------+-------------+
| Bedroom1   |      2 |       1 |          1 |           2 |
| Bedroom2   |      4 |       0 |          4 |           0 |
| LivingRoom |      2 |       2 |          3 |           1 |
+------------+--------+---------+------------+-------------+

我知道您需要count函数来获取出现次数,例如

data.groupby('Room')['TV','Lights']。count()

但是我如何将其分为开和关计数的不同列。我也尝试使用聚合,但无法使其正常工作。

1 个答案:

答案 0 :(得分:1)

好吧,这毕竟是一个groupby问题。首先,以与最终结果更相似的格式获取df:

df2 = pd.DataFrame({'Room': df.Room, 'Lights(On)': df.Lights == 'On', 'Lights(Off)': df.Lights == 'Off'})

    Lights(Off)  Lights(On)      Room
0         True       False  Bedroom1
1        False        True  Bedroom2
2        False        True  Bedroom2


result = df2.groupby('Room').sum()


          Lights(Off)  Lights(On)
Room
Bedroom1          1.0         0.0
Bedroom2          0.0         2.0