-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.py
259 lines (227 loc) · 8.81 KB
/
result.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#############################
# Author: Gary Loayza #
# Date: 2020-04-29 #
#############################
import datetime as dt
import pandas as pd
import numpy as np
import main
today = dt.date.today()
stamp = today.strftime("%Y-%m-%d")
def init(data):
"""
Generate a dictionary of empty DataFrames by branch to
store later data points.
"""
results = {}
results['Total'] = pd.DataFrame([], columns=['Description','Total'])
for index, row in data.iterrows():
results[row['description']] = pd.DataFrame([], columns=['Description','Total'])
return results
def tot_members(data, results):
"""
Handle writing to results for membership distribution
"""
for tab in results:
results[tab] = results[tab].append({
'Description': 'Members',
'Total': ''
}, ignore_index=True)
results['Total'] = results['Total'].append({
'Description': 'Total Members',
'Total': data['person_serial'].nunique()
}, ignore_index=True)
for tab in results:
results[tab] = results[tab].append({
'Description': 'Total Members',
'Total': data[data['branch']==tab]['person_serial'].nunique()
}, ignore_index=True)
results['Total'].drop_duplicates(subset='Description',keep='first',inplace=True)
return results
def ages(data, results):
"""
Compute age breakdowns for each branch
"""
age = ((today - data['birth_date'])/dt.timedelta(days=365.25)).dropna().astype(int)
results['Total'] = results['Total'].append({
'Description': 'Average Age',
'Total': '%.2f'%(age.mean())
}, ignore_index=True)
results['Total'] = results['Total'].append({
'Description': 'Ages 0 to 17',
'Total': age.apply(lambda x: np.sum(x<=17)).sum()
}, ignore_index=True)
results['Total'] = results['Total'].append({
'Description': 'Ages 18 to 55',
'Total': age.apply(lambda x: np.sum(17<x<=55)).sum()
}, ignore_index=True)
results['Total'] = results['Total'].append({
'Description': 'Ages 56 and Up',
'Total': age.apply(lambda x: np.sum(x>55)).sum()
}, ignore_index=True)
age = {}
for tab in results:
age[tab] = (
today - data[data['branch'] == tab]['birth_date']
).apply(
lambda x: x/dt.timedelta(days=365.25)
).dropna().astype(int)
results[tab] = results[tab].append({
'Description': 'Average Age',
'Total': '%.2f'%(age[tab].mean())
}, ignore_index=True)
results[tab] = results[tab].append({
'Description': 'Ages 0 to 17',
'Total': age[tab].apply(lambda x: np.sum(x<=17)).sum()
}, ignore_index=True)
results[tab] = results[tab].append({
'Description': 'Ages 18 to 55',
'Total': age[tab].apply(lambda x: np.sum(17<x<=55)).sum()
}, ignore_index=True)
results[tab] = results[tab].append({
'Description': 'Ages 56 and Up',
'Total': age[tab].apply(lambda x: np.sum(x>55)).sum()
}, ignore_index=True)
results['Total'].drop_duplicates(subset='Description',keep='first',inplace=True)
return results
def loans(data, results):
"""
Compute loan totals and balances for each branch
"""
for tab in results:
results[tab] = results[tab].append({
'Description': 'Loans',
'Total': ''
}, ignore_index=True)
results['Total'] = results['Total'].append({
'Description': 'Total Loans',
'Total': data.shape[0]
}, ignore_index=True)
results['Total'] = results['Total'].append({
'Description': 'Total Balance',
'Total': '$%.2f'%(data['balance'].sum())
}, ignore_index=True)
for tab in results:
results[tab] = results[tab].append({
'Description': 'Total Loans',
'Total': data[data['branch']==tab].shape[0]
}, ignore_index=True)
results[tab] = results[tab].append({
'Description': 'Total Balance',
'Total': '$%.2f'%(data[data['branch']==tab]['balance'].sum())
}, ignore_index=True)
results['Total'].drop_duplicates(subset='Description',keep='first',inplace=True)
return results
def products(data, results, flavor):
"""
Compute share/loan product totals and per branch breakdown,
depending on the flavor when run.
"""
if flavor == 'Share':
for tab in results:
results[tab] = results[tab].append({
'Description': 'Products',
'Total': ''
}, ignore_index=True)
for tab in results:
results[tab] = results[tab].append({
'Description': flavor + ' ' + 'Products',
'Total': ''
}, ignore_index=True)
products = data['description'].value_counts().reset_index()
products.columns = ['Description','Total']
results['Total'] = results['Total'].append(products, ignore_index=True)
products = {}
for tab in results:
products[tab] = data[data['branch']==tab]['description'].value_counts().reset_index()
products[tab].columns = ['Description', 'Total']
results[tab] = results[tab].append(products[tab], ignore_index=True)
return results
def safety_deposit_boxes(data, results):
"""
Display the availability of all safety deposit boxes at each branch
"""
for tab in results:
results[tab] = results[tab].append({
'Description': 'Services',
'Total': ''
}, ignore_index=True)
results[tab] = results[tab].append({
'Description': 'Safety Deposit Boxes',
'Total': ''
}, ignore_index=True)
sdb = data['status'].value_counts().reset_index()
sdb.columns = ['Description', 'Total']
results['Total'] = results['Total'].append(sdb, ignore_index=True)
sdb = {}
for tab in results:
sdb[tab] = data[data['branch']==tab]['status'].value_counts().reset_index()
sdb[tab].columns = ['Description', 'Total']
results[tab] = results[tab].append(sdb[tab], ignore_index=True)
return results
def estatements(data, results):
"""
Display the usage of eStatements
Note that eStatements are per account, not per branch
"""
results['Total'] = results['Total'].append({
'Description': 'eStatements',
'Total': ''
}, ignore_index=True)
estat_counts = data['e_stmt_option'].value_counts().reset_index()
estat_counts.columns = ['Description', 'Total']
estat_counts.index = estat_counts['Description']
results['Total'] = results['Total'].append(estat_counts, ignore_index=True)
results['Total'] = results['Total'].append({
'Description': 'Total Accounts subscribed to eStatments',
'Total': estat_counts['Total'].loc['E-statement only']
+ estat_counts['Total'].loc['E-statement and mail statement']
}, ignore_index=True)
return results
def insurance(data, results):
"""
Compute the insurance products and break them down by branch
"""
for tab in results:
results[tab] = results[tab].append({
'Description': 'Insurance',
'Total': ''
}, ignore_index=True)
life = data['life_insurance'].value_counts().reset_index()
life.columns = ['Description', 'Total']
results['Total'] = results['Total'].append(life, ignore_index=True)
other = data['other_insurance'].value_counts().reset_index()
other.columns = ['Description', 'Total']
results['Total'] = results['Total'].append(other, ignore_index=True)
# Split by Branch
life = {}
other = {}
for tab in results:
life[tab] = data[data['branch']==tab]['life_insurance'].value_counts().reset_index()
life[tab].columns = ['Description', 'Total']
results[tab] = results[tab].append(life[tab], ignore_index=True)
other[tab] = data[data['branch']==tab]['other_insurance'].value_counts().reset_index()
other[tab].columns = ['Description', 'Total']
results[tab] = results[tab].append(other[tab], ignore_index=True)
return results
def login(data, results):
"""
Display login methods that our users are utilizing
Note that login data is not even attached to a member
"""
results['Total'] = results['Total'].append({
'Description': 'Phone Banking',
'Total': ''
}, ignore_index=True)
data.columns = ['Description', 'Total']
results['Total'] = results['Total'].append(data, ignore_index=True)
return results
def printr(results):
"""
Method for writing results DataFrames to spreadsheet
"""
with pd.ExcelWriter('/opt/program/output/Monthly_Member_Analysis_' + stamp + '.xlsx') as writer:
for tab in results:
results[tab].to_excel(writer, sheet_name=tab, index=False)
if __name__ == "__main__":
main.program()