forked from NaponTunglukmongkol/IndustrialAccident
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcause.py
More file actions
174 lines (104 loc) · 3.53 KB
/
cause.py
File metadata and controls
174 lines (104 loc) · 3.53 KB
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
import pandas as pd
df = pd.read_csv('ProjectPsit1.csv')
years = df.ปี
year_all = []
for year in years:
if year not in year_all:
year_all.append(year)
year_all
def kindofaccident():
accidents = df.accident
accident_all = []
for accident in accidents:
if accident not in accident_all:
accident_all.append(accident)
return accident_all
def injured_dicc():
injured_dic = {}
injured = df.injured
year_b = year_all[0]
people_injured = 0
for i in year_all:
injured_dic[year_b] = people_injured
people_injured = 0
for people, year in zip(injured,years):
if year == i:
people_injured += people
year_b = i
injured_dic[year_b] = people_injured
list_injured = [injured_dic[year] for year in year_all]
return list_injured
def death_year():
death = df.death
death_dic = {}
people_death = 0
year_b = year_all[0]
for i in year_all:
death_dic[year_b] = people_death
people_death = 0
for people, year in zip(death, years):
if year == i:
people_death += people
year_b = i
death_dic[year_b] = people_death
list_death = [death_dic[year] for year in year_all]
return list_death
def accident_year():
accident_all = df.accident
count_accident = 0
dic_acc = {}
accident_b = 0
for accident in kindofaccident():
dic_acc[accident_b] = count_accident
count_accident = 0
for accidents in accident_all:
if accident == accidents:
count_accident += 1
accident_b = accident
dic_acc[accident_b] = count_accident
return dic_acc
summary_all = df.summary
summary_list = []
for summary in summary_all:
if summary not in summary_list:
summary_list.append(summary)
accident_all = df.accident
# In[14]:
def accident_count(accident_choice):
summary_fire = {}
summary_b = "0"
count = 0
for summary in summary_list:
if count > 0:
summary_fire[summary_b] = count
count = 0
for accident, summary_s in zip(accident_all, summary_all):
if accident == accident_choice and summary_s == summary:
count += 1
summary_b = summary
summary_fire[summary_b] = count
a = sorted(summary_fire.values())
a.sort(reverse=True)
list_top_4 = []
if len(list_top_4) > 4:
for i in range(4):
list_top_4.append(list(summary_fire.keys())[list(summary_fire.values()).index(a[i])])
else:
for i in range(3):
list_top_4.append(list(summary_fire.keys())[list(summary_fire.values()).index(a[i])])
count = 0
dic_summary_top_4 = {}
for summary in list_top_4:
dic_summary_top_4[summary] = summary_fire[summary]
count += summary_fire[summary]
if accident_year()[accident_choice] - count != 0:
dic_summary_top_4["อื่นๆ"] = accident_year()[accident_choice] - count
import pygal
pie_chart = pygal.Pie()
pie_chart.title ='สาเหตหลักของการเกิด'+accident_choice+' %'
for summary in dic_summary_top_4:
if dic_summary_top_4[summary] != 0:
percent = round((dic_summary_top_4[summary])/(accident_year()[accident_choice])*100, 2)
pie_chart.add(summary, percent)
pie_chart.render_to_file('pie_chart.svg')
accident_count(input())