-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
181 lines (147 loc) · 5.37 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import typing as T
import pandas as pd
import numpy as np
def get_employees_df():
return pd.read_csv(
"https://gist.githubusercontent.com/kevin336/acbb2271e66c10a5b73aacf82"
"ca82784/raw/e38afe62e088394d61ed30884dd50a6826eee0a8/employees.csv")
def get_departments_df():
dep_df = pd.read_csv(
"https://gist.githubusercontent.com/kevin336/5ea0e96813aa88871c20d315b5"
"bf445c/raw/d8fcf5c2630ba12dd8802a2cdd5480621b6a0ea6/departments.csv")
dep_df = dep_df.rename(columns={"DEPARTMENT_ID": "DEPARTMENT_IDENTIFIER"})
return dep_df
#
## Task 1
#
def salary_statistics(
*,
employees_df: pd.DataFrame
) -> T.Dict[str, np.float64]:
"""
Calculate the average, median, lower and upper quartiles of an employees' salaries.
"""
avg_salary = employees_df["SALARY"].mean()
median_salary = employees_df["SALARY"].median()
lower_quartile = employees_df["SALARY"].quantile(0.25)
upper_quartile = employees_df["SALARY"].quantile(0.75)
return {
'average': avg_salary,
'median': median_salary,
'lower_quartile': lower_quartile,
'upper_quartile': upper_quartile
}
#
## Task 2
#
def avg_salary_per_department(
*,
employees_df: pd.DataFrame,
departments_df: pd.DataFrame
)-> pd.DataFrame:
"""
calculate the average salary per department. Please include the department name in the results
"""
avg_salary_per_depart_df = employees_df.groupby(["DEPARTMENT_ID"])["SALARY"].mean().reset_index()
avg_salary_per_depart_df = avg_salary_per_depart_df.rename(columns={"SALARY": "AVG_SALARY"})
avg_salary_per_department_df = pd.merge(
avg_salary_per_depart_df,
departments_df,
left_on="DEPARTMENT_ID",
right_on="DEPARTMENT_IDENTIFIER"
)
return avg_salary_per_department_df[["DEPARTMENT_ID", "DEPARTMENT_NAME", "AVG_SALARY"]]
#
## Task 3
#
def create_salary_category(
*,
employees_df: pd.DataFrame,
avg_salary: np.float64
) -> pd.DataFrame:
"""
Create a new column named `SALARY_CATEGORY` with value "low"
when the salary is lower than average and "high" if is it higher or equal.
"""
employees_df["SALARY_CATEGORY"] = employees_df["SALARY"] \
.apply(lambda salary: "low" if (salary < avg_salary) else "high")
return employees_df
#
## Task 4
#
def create_salary_category_among_department(
*,
employees_df: pd.DataFrame,
avg_salary_per_department_df: pd.DataFrame
) -> pd.DataFrame:
"""
Create another column named `SALARY_CATEGORY_AMONG_DEPARTMENT` with value
"low" when the employee salary is lower than average in his / her department and "high" in the other case.
"""
# TODO it seems that the merge results in two salary category
# This is not expected. An investigation must be conducted to understand the root cause of this behaviour
merged_df = employees_df.merge(avg_salary_per_department_df, how="inner", on="DEPARTMENT_ID")
merged_df["SALARY_CATEGORY_AMONG_DEPARTMENT"] = merged_df \
.apply(lambda e: "low" if (e["SALARY"] < e["AVG_SALARY"]) else "high", axis=1)
return merged_df[[*employees_df.columns, "SALARY_CATEGORY", "AVG_SALARY", "SALARY_CATEGORY_AMONG_DEPARTMENT"]]
#
## Task 5
#
def filter_employees_depart_id_20(
*,
employees_df: pd.DataFrame
) -> pd.DataFrame:
"""
Filter the dataframe `employees` to include only the rows where `DEPARTMENT_ID` equals to 20. Assign the result to new variable.
"""
return employees_df[employees_df["DEPARTMENT_ID"] == 20]
#
## Task 6
#
def increase_salary_10_perc(
*,
employees_of_depart_id_20_df: pd.DataFrame
) -> pd.DataFrame:
"""
Increase the salary by 10% for all employees working at the department 20.
"""
# Note: The deep copy is used to avoid changing the original dataframe
# Otherwise, we could directly work on the original dataframe
employees_of_depart_id_20_df_copied = employees_of_depart_id_20_df.copy(deep=True)
employees_of_depart_id_20_df_copied['SALARY'] *= 1.10
return employees_of_depart_id_20_df_copied
#
## Task 7
#
def is_any_phone_number_empty(
*,
employees_df: pd.DataFrame
) -> bool:
"""
Check if any of the `PHONE_NUMBER` column values are empty.
"""
return employees_df['PHONE_NUMBER'].isnull().any() or (employees_df['PHONE_NUMBER'].str.strip() == '').any()
if __name__ == "__main__":
employees_df = get_employees_df()
departments_df = get_departments_df()
salary_stats = salary_statistics(employees_df=employees_df)
avg_salary_per_department_df = avg_salary_per_department(
employees_df=employees_df,
departments_df=departments_df
)
employees_df = create_salary_category(
employees_df=employees_df,
avg_salary=salary_stats["average"]
)
employees_df = create_salary_category_among_department(
employees_df=employees_df,
avg_salary_per_department_df=avg_salary_per_department_df
)
employees_of_depart_id_20_df = filter_employees_depart_id_20(employees_df=employees_df)
employees_of_depart_id_20_df = increase_salary_10_perc(
employees_of_depart_id_20_df=employees_of_depart_id_20_df
)
if is_any_phone_number_empty(employees_df=employees_df):
print("Some phone numbers are missing!")
else:
print("All phone numbers are provided!")