Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.8 kB
2
Indexable
Never
#Recommended for editing Excel/csv files - pandas library
import pandas as pd

# Part 1 : Read the CSV file

#df : dataframe
df = pd.read_csv('Events_All.csv')

#Part 2 : Extract the useful info - empId and alert

data = df[["empId","alert"]]

# Part 3 : % no of alerts per employee to total:

# df.shape[0] : number of rows 
total_alerts = data.shape[0]

total_alerts_per_employee = pd.DataFrame(data.groupby('empId').count().reset_index())
percentage_emp_alerts_to_total_alerts = total_alerts_per_employee
percentage_emp_alerts_to_total_alerts['percentage_emp_alerts_to_total_alerts (%)'] = total_alerts_per_employee['alert'] * 100 / total_alerts
print(percentage_emp_alerts_to_total_alerts)
percentage_emp_alerts_to_total_alerts.to_csv('percentage_emp_alerts_to_total_alerts.csv', encoding='utf-8', index=False)

# Part 4 : % of sub alerts per employee to each employee

sub_alerts_per_employee = data.groupby(['empId','alert']).size().reset_index(name='sub_alerts_per_employee')
total_alerts_per_employee = sub_alerts_per_employee.groupby('empId')['sub_alerts_per_employee'].agg('sum').reset_index(name='total_alerts_per_employee')

# inner : intersection of two dataframes
sub_and_total_alerts_per_emp = pd.merge(sub_alerts_per_employee, total_alerts_per_employee, how='inner')
print(sub_and_total_alerts_per_emp)
percentage_sub_and_total_alerts_per_emp = sub_and_total_alerts_per_emp
percentage_sub_and_total_alerts_per_emp['percentage_sub_and_total_alerts_per_emp (%)'] = sub_and_total_alerts_per_emp['sub_alerts_per_employee'] * 100 / sub_and_total_alerts_per_emp['total_alerts_per_employee']
print(percentage_sub_and_total_alerts_per_emp)
percentage_sub_and_total_alerts_per_emp.to_csv('percentage_sub_and_total_alerts_per_emp.csv', encoding='utf-8', index=False)