-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.py
90 lines (66 loc) · 2.59 KB
/
reader.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
""" This document defines the functionality to read data from the excel files """
from math import isnan
import pandas as pd
import xlsxwriter as xw
class Reader():
"""The Reader class receives the path of the excel to be read and
defines contains the functionality to read and process data from excel
"""
def __init__(self, path):
self.path = path
def clean(self, dict_with_nan):
""" cleans dictionary for it to not contain NaN values """
clean = {}
for key in dict_with_nan:
clean[key] = [h for h in dict_with_nan[key] if type(h) is str or not isnan(h)]
return clean
def read_doctors(self):
"""doctors are read from the provided path. This function
takes care of some of the formating.
"""
doctors = pd.read_excel(self.path, "Sheet1")
doctors.drop(doctors.columns[[0,1,2,3]], axis=1, inplace=True)
#doctors = doctors.apply(lambda x: str(x).strip())
doctors["Vorname"] = doctors["Vorname"] + " " + doctors["Nachname"]
doctors.drop(columns=["Nachname"], axis=1, inplace=True)
hospitals = pd.read_excel("hospitals.xlsx")["Name"].apply(lambda x: x.strip())
hospital_dfs = []
for hospital in hospitals:
hospital = hospital.strip()
hospital_overview = []
for index, row in doctors.iterrows():
for i, prio in enumerate(list(row[1:])):
if prio == hospital:
current = {}
current["Vorname"] = row["Vorname"]
current["Prio"] = "Prio {}".format(i)
hospital_overview.append(current)
hospital_dfs.append(pd.DataFrame(hospital_overview))
#writer = pd.ExcelWriter("candidates.xlsx", engine="xlsxwriter")
#for i, hospital_df in enumerate(hospital_dfs):
# hospital_df.to_excel(writer, sheet_name=hospitals[i])
# writer.save()
doctors_dict = doctors.set_index("Vorname").T.to_dict("list")
return self.clean(doctors_dict)
def read_hospital_capacities(self):
hospital_dict = pd.read_excel("hospitals.xlsx")
names = hospital_dict["Name"].apply(lambda x: x.strip())
capacities = hospital_dict["Capacities"]
capacity_dict = {}
for i in range(0, len(names)):
capacity_dict[names[i]] = capacities[i]
return capacity_dict
def read_hospital_prefs(self):
hospitals = pd.read_excel("hospitals.xlsx")["Name"].apply(lambda x: x.strip())
dict_hospital_prefs = {}
for hospital in hospitals:
hospital_prefs = pd.read_excel("candidates.xlsx", sheet_name=hospital)["Vorname"]
hospital_prefs = hospital_prefs.apply(lambda x: x.strip())
dict_hospital_prefs[hospital] = list(hospital_prefs)
return self.clean(dict_hospital_prefs)
def turn_dict(self, d):
new_d = {}
for key in d:
for v in d[key]:
new_d[v] = key
return new_d