我有一个具有以下结构的数据框,
| Date | Item | Location | Event |
|------------|-------:|----------|---------|
| 01-06-2019 | Item_1 | Loc_1 | Event_1 |
| 01-06-2019 | Item_1 | Loc_1 | Event_1 |
| 02-06-2019 | Item_1 | Loc_1 | Event_1 |
| 02-06-2019 | Item_1 | Loc_1 | Event_2 |
| 02-06-2019 | Item_1 | Loc_2 | Event_2 |
| 02-06-2019 | Item_2 | Loc_1 | Event_3 |
| 03-06-2019 | Item_2 | Loc_1 | Event_3 |
| 03-06-2019 | Item_2 | Loc_1 | Event_3 |
我想根据 Item + Location 计算一天中发生的事件数。 结果如下,
| Date | Item | Location | Event_1 | Event_2 | Event_3 |
|------------|-------:|----------|---------|---------|---------|
| 01-06-2019 | Item_1 | Loc_1 | 2 | 0 | 0 |
| 02-06-2019 | Item_1 | Loc_1 | 1 | 1 | 0 |
| 02-06-2019 | Item_1 | Loc_2 | 0 | 1 | 0 |
| 02-06-2019 | Item_2 | Loc_1 | 0 | 0 | 1 |
| 03-06-2019 | Item_2 | Loc_1 | 0 | 0 | 2 |
试过pandas pivot_table,得不到我想要的结果。
谢谢!
最佳答案
使用crosstab
与 DataFrame.reset_index
:
df1 = pd.crosstab([df['Date'], df['Item'], df['Location']], df['Event']).reset_index()
print (df1)
Event Date Item Location Event_1 Event_2 Event_3
0 01-06-2019 Item_1 Loc_1 2 0 0
1 02-06-2019 Item_1 Loc_1 1 1 0
2 02-06-2019 Item_1 Loc_2 0 1 0
3 02-06-2019 Item_2 Loc_1 0 0 1
4 03-06-2019 Item_2 Loc_1 0 0 2
替代方案:
df1=df.groupby(['Date','Item','Location','Event']).size().unstack(level = -1,fill_value=0).reset_index()
关于Python pandas 按多列分组的行总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56785422/