-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_accidents.py
53 lines (39 loc) · 2.16 KB
/
process_accidents.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
import pandas as pd
import logging
import os
# ----------------------------------------------------------------------------------- #
# Define constants #
# ----------------------------------------------------------------------------------- #
# Filepaths
EXP_FILEPATH = os.path.join('data', 'input', 'expeditions.csv')
ACCIDENT_FILEPATH = os.path.join('data', 'output', 'accident_reports.csv')
NO_EXPED_FILEPATH = os.path.join('data', 'output', 'no_exped.csv')
# Other
NO_PEAKS = 5 # Number of peaks to plot
ORIG_LOG_CONFIG = logging.getLogger().getEffectiveLevel()
# ----------------------------------------------------------------------------------- #
# Configurations #
# ----------------------------------------------------------------------------------- #
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
# ----------------------------------------------------------------------------------- #
# Initial Data Processing #
# ----------------------------------------------------------------------------------- #
# Load expeditions data
exp_df = pd.read_csv(EXP_FILEPATH)
exp_df.dropna(subset=['peakid'], inplace=True, ignore_index=True)
# Create unique expid column
exp_df['expid'] = exp_df['expid'] + '-' + exp_df['year'].astype('str')
# Find peaks with the highest number of expeditions and extract the data
key_df = exp_df.groupby(by=['peakid'])['expid'].count().reset_index()
key_df.rename(columns={'expid': 'no_exped'}, inplace=True)
key_df.sort_values(by='no_exped', ascending=False, inplace=True, ignore_index=True)
key_df = key_df.iloc[:NO_PEAKS, :]
# Save number of expeditions for plotting
key_df.to_csv(NO_EXPED_FILEPATH, index=False)
acc_df = key_df[['peakid']].merge(exp_df[['peakid', 'accidents']], how='left', on='peakid')
acc_df.dropna(subset=['accidents'], inplace=True, ignore_index=True)
# Save acc_df (one time only)
acc_df.reset_index(inplace=True)
acc_df.rename(columns={'index': 'acc_id'}, inplace=True)
acc_df.to_csv(ACCIDENT_FILEPATH, index=False)