I am trying to add a pivot table in excel using python script with pandas but not able to do so. I won’t to count number of missed and met entries for each priority.
Excel script:
JavaScript
x
3
1
import pandas as pd
2
df = pd.DataFrame({'Priority': ['p1','p2','p3','p2','p3'],'SLA': ['Met','Missed','Missed','Met','Missed']})
3
Excel data:
Priority | SLA |
---|---|
p1 | Met |
p2 | Missed |
p3 | Missed |
p2 | Missed |
p3 | Missed |
desired output:
Priority | Met | Missed |
---|---|---|
p1 | 1 | 0 |
p2 | 1 | 1 |
p3 | 0 | 2 |
I tried different combinationapproach with
JavaScript
1
2
1
table = pd.pivot_table(df,index='Priority',columns=['SLA'])
2
but couldn’t get it right. I am struggling hard for this. I am trying this first time.
Advertisement
Answer
We need to understand how pandas.DataFrame.pivot_table
works in order to solve this problem.
First, it has three different inputs:
- Values -> the values on which the aggregation happens. In this case, it is SLA.
- Columns -> the new columns that are to be created. In this case, it’s SLA’s values.
- Index -> the rows that are to be kept. In this case, it’s Priority.
Let’s convert this into code.
JavaScript
1
14
14
1
df.pivot_table(
2
# SLA's values as the columns
3
columns=df['SLA'].values,
4
5
# Priority as the rows.
6
index=['Priority'],
7
8
# SLA's values as the values to be aggregated upon (counted).
9
values=['SLA'],
10
11
# Count is our aggregate function
12
aggfunc='count'
13
).fillna(0).astype('int') # Then we fill NaN values with 0, and convert the df -> int
14