forked from FilippoBovo/production-data-science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
54 lines (45 loc) · 1.26 KB
/
data.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
import logging
def extract_title(df):
"""Extract the title from the passenger names.
Parameters
----------
df : pandas.DataFrame
Data-frame containing the column `Name`
Returns
-------
pandas.DataFrame
Data-frame with additional column with titles
"""
logging.info("Extracting the titles from the name column")
simplify_title = {
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir": "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess": "Royalty",
"Dona": "Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr": "Mr",
"Mrs": "Mrs",
"Miss": "Miss",
"Master": "Master",
"Lady": "Royalty"
}
title = df['Name'].apply(
lambda full_name: (
simplify_title[
# Example: Uruchurtu, Don. Manuel E --> Don
full_name.split(',')[1].split('.')[0].strip()
]
)
)
merged = df.merge(title.to_frame(name='Title'),
left_index=True, right_index=True)
merged['Title'] = merged['Title'].astype('category')
return merged