-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1a_parse.py
107 lines (85 loc) · 4.71 KB
/
1a_parse.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
import pandas as pd
import argh
import datetime
import os
import sys
def main():
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
file_name = sys.argv[1]
df = pd.read_csv(file_name)
#Total cases is just number of rows in the csv
total_cases = len(df)
#Get all cases where cash bail was set
all_cash_bail = df[df['Bail Type'].fillna("").str.contains('Monetary')]
total_cash_bail = (len(all_cash_bail))
percentage_cash_bail = int((total_cash_bail / total_cases) * 100)
#Get all ROR cases
all_ror = df[df['Bail Type'].fillna("").str.contains('ROR')]
total_ror = (len(all_ror))
percentage_ror = int((total_ror / total_cases) * 100)
#Get all Unsecured cases
all_unsecured = df[df['Bail Type'].fillna("").str.contains('Unsecured')]
total_unsecured = (len(all_unsecured))
percentage_unsecured = int((total_unsecured / total_cases) * 100)
# Get all ROR cases
all_denied = df[df['Bail Type'].fillna("").str.contains('Denied')]
total_denied = (len(all_denied))
percentage_denied = int((total_denied / total_cases) * 100)
##Now some stats on the cases where cash bail was set
#Get all where bail was posted
all_posted = all_cash_bail[all_cash_bail['Bail Status'].fillna("").str.contains('Posted')]
total_posted = len(all_posted)
percentage_posted = int((total_posted / total_cash_bail) * 100)
#Get all with Public Defender
all_defenders = all_cash_bail[all_cash_bail['Represented'].str.contains('Defender Association', na=False)]
total_defenders = len(all_defenders)
percentage_defenders = int((total_defenders / total_cash_bail) * 100)
#Get just bail amounts
all_cash_bail_amounts = all_cash_bail['Bail Amount']
all_cash_bail_amounts_clean = all_cash_bail_amounts.apply(clean_up_money)
values = all_cash_bail_amounts_clean.astype(int)
list = values.sort_values().values
#min bail
min_bail = list[0]
min_needed_to_post = int(min_bail / 10)
min_bail_formatted = "${:,}".format(min_bail)
min_needed_to_post_formatted = "${:,}".format(min_needed_to_post)
#max bail
max_bail = list[-1]
max_needed_to_post = int(max_bail / 10)
max_bail_formatted = "${:,}".format(max_bail)
max_needed_to_post_formatted = "${:,}".format(max_needed_to_post)
#average bail
avg_bail = int(sum(list) / len(list))
avg_needed_to_post = int(avg_bail / 10)
avg_bail_formatted = "${:,}".format(avg_bail)
avg_needed_to_post_formatted = "${:,}".format(avg_needed_to_post)
#total bail
total_bail = sum(list)
total_needed_to_post = int(total_bail / 10)
total_bail_formatted = "${:,}".format(total_bail)
total_needed_to_post_formatted = "${:,}".format(total_needed_to_post)
#construct the message
total = 'Total # Cases Arraigned: %s' % total_cases
cash_bail = 'Cash bail: {0}% ({1} cases)*'.format(percentage_cash_bail, total_cash_bail)
ror = 'ROR: {0}% ({1} cases)'.format(percentage_ror, total_ror)
unsecured = 'Unsecured: {0}% ({1} cases)'.format(percentage_unsecured, total_unsecured)
denied = 'Denied: {0}% ({1} cases)'.format(percentage_denied, total_denied)
cases_bail_set = 'Of the {0} cases where bail was set:'.format(total_cash_bail)
number_posted = '-{0} were posted'.format(total_posted)
public_defender = '-in {0}% ({1} cases) a public defender was assigned due to indigence'.format(percentage_defenders,
total_defenders)
highest = 'Highest cash bail: {0} ({1} needed to post bail)'.format(max_bail_formatted, max_needed_to_post_formatted)
lowest = 'Lowest cash bail: {0} ({1} needed to post bail)'.format(min_bail_formatted, min_needed_to_post_formatted)
average = 'Average bail issued: {0} ({1} needed to post bail)'.format(avg_bail_formatted, avg_needed_to_post_formatted)
final_sum = 'Total cash bail issued: {0} ({1} needed to post bail for all)'.format(total_bail_formatted,
total_needed_to_post_formatted)
yesterdayFormatted = yesterday.strftime("%B %d, %Y")
topPart = 'Philadelphia | {0}'.format(yesterdayFormatted)
MESSAGE = topPart + "\n" + total + "\n" + "\n" + cash_bail + "\n" + ror + "\n" + unsecured + "\n" + denied + "\n" + "\n" + cases_bail_set + "\n" + number_posted + "\n" + public_defender + "\n" + "\n" + highest + "\n" + lowest + "\n" + average + "\n" + final_sum
print(MESSAGE)
def clean_up_money(x):
no_dollar_sign = x[1:len(x) - 3]
return no_dollar_sign.replace(',', '')
main()