'Python script summary data separate by col
I want to count the data base on certain column follow the condition from another columns.
Raw data:
| Time | Lot | Error |
|---|---|---|
| 1:00 | A | X |
| 2:00 | A | X |
| 2:12 | B | X |
| 2:30 | B | Y |
| 4:29 | C | Z |
Desired data show:
| Lot | Error | Count |
|---|---|---|
| A | X | 2 |
| B | X | 1 |
| B | Y | 1 |
| C | Z | 1 |
I tried using by "groupby" and "pivot" but it not work.
table = df.groupby(['Lot', 'Name']).sum('Name')
Solution 1:[1]
Try count(), for both columns.
df.groupby(['Lot', 'Error']).count()
Output Lot Error
A X 2
B X 1
Y 1
C Z 1
Also, if you need all the indexes that you have given, then you can do this:
df1 = df.groupby(['Lot', 'Error']).count().reset_index()
df1.rename(columns={'Time': 'count'}, inplace=True)
Output
Lot Error count
0 A X 2
1 B X 1
2 B Y 1
3 C Z 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
