-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare.py
80 lines (62 loc) · 2.66 KB
/
prepare.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
import pandas as pd
import numpy as np
import re
def get_manufacturer(model):
if model.startswith('ST'):
return 'Seagate'
elif model.startswith('Sea'):
return 'Seagate'
elif model.startswith('HGST'):
return 'Hitachi'
elif model.startswith('Hit'):
return 'Hitachi'
elif model.startswith('TOS'):
return 'Toshiba'
elif model.startswith('DEL'):
return 'Dell'
elif model.startswith('WD'):
return 'Western Digital'
elif model.startswith('Sam'):
return 'Samsung'
elif model.startswith('SAM'):
return 'Samsung'
else:
return 'Unknown'
def prepare(df):
# Convert capacity column from bytes to terabytes
df['capacity_bytes'] = round((df['capacity_bytes']/ 1_000_000_000_000),1)
# Convert power hours to years
df['max(smart_9_raw)'] = round((df['max(smart_9_raw)']/ 8760),1)
# Create a new column for manufacturer
df['manufacturer'] = df.model.apply(get_manufacturer)
# Rename columns appropriately
df = df.rename(columns={'capacity_bytes':'capacity_terabytes',
'max(failure)':'failure',
'max(smart_9_raw)':'drive_age_in_years',
'max(smart_5_raw)':'reallocated_sectors_count',
'max(smart_187_raw)':'reported_uncorrectable_errors',
'max(smart_188_raw)':'command_timeout',
'max(smart_197_raw)':'current_pending_sector_count',
'max(smart_198_raw)':'uncorrectable_sector_count'})
# Reorder columns
df = df[['serial_number','manufacturer','model','capacity_terabytes',
'failure','drive_age_in_years','reallocated_sectors_count',
'reported_uncorrectable_errors','command_timeout',
'current_pending_sector_count','uncorrectable_sector_count']]
return df
def unique(df):
'''
Remove rows that duplicate serial numbers after aggregation.
'''
# remove rows with 0 copacity_terabytes they were creating duplicate rows for some of the serial numbers
df = df[df.capacity_terabytes > 0]
# remove rows by index that were creating duplicate serial numbers
df = df.drop([68273,50408,37150,154660,162948,50816,156370,31687,20823,75191,132189,4177,78104,103620,141505,95249,26969])
return df
def treat_nulls(df):
# remove nulls from specific columns by imputing zeroes
df.reported_uncorrectable_errors = df.reported_uncorrectable_errors.fillna(value=0)
df.command_timeout = df.command_timeout.fillna(value=0)
# treat remaining nulls by dropping specific rows
df = df.dropna(axis=0)
return df