# coding=utf-8

# Load train/test datasets as Pandas DataFrames
df_train = pd.read_csv('Data/train.csv', index_col='PassengerId')
df_test = pd.read_csv('Data/test.csv', index_col='PassengerId')
df = pd.concat([df_train, df_test], axis=0)

# Add New Feature (family_size)
# `family_size` is calculated using the following features: `sibsp` & `parch`
df.family_size = df.sibsp + df.parch + 1

# Add New Feature (no_family)
# Feature reflects passengers that have no family members on-board:
# `family_size` equals 1 -> T/F -> 1/0 ==> `no_family`
df.no_family = np.array(df.family_size == 1).astype(int)

# Add New Feature (name_length)
# `name_length` equals string length of `name` feature.
df.name_length = df.name.str.len()

# Add New Feature (name_length_group)
# `name_length -> 4x bins ==> `name_length_group`
df.name_length_group = pd.cut(df.name_length, 4, labels=False)

# Add New Feature (deck)
# `deck` is calculated by extracting the first character of 'cabin' feature.
# If 'cabin' is null, than `deck` value for that observation is set to '?'.
df.deck = df.cabin.fillna('?').str.slice(0, 1)


@@ -1097,8 +1097,834 @@
 (0, 1)%0A%0A
+'''%0AThe resulting feature %60title%60 is obtained by performing the %0Afollowing operations on %60name%60:%0A (1) splitting %5E*(string) on ',' & extracting 1st item%0A (2) splitting %5E(1) on '.' & extracting 1st item%0A (3) stripping %5E(2) of any leading/trailing whitespace characters%0A (1) mapping %5E(3) to title groups using %60title_map%60(dict)%0A'''%0Adef kv_to_vk(d):%0A    return %7Bt: k for k, v in d.items() for t in v%7D%0A%0Atitle_map = kv_to_vk(%7B%0A    'Officer': %5B'Capt', 'Col', 'Major', 'Rev'%5D,%0A    'Royalty': %5B'Sir', 'Lady', 'the Countess',%0A                'Dona', 'Jonkheer', 'Don'%5D,%0A    'Dr': %5B'Dr'%5D, 'Master': %5B'Master'%5D, 'Mr': %5B'Mr'%5D,%0A    'Miss': %5B'Ms', 'Miss', 'Mlle'%5D, 'Mrs': %5B'Mrs', 'Mme'%5D%7D)%0Adf.title = (df.name%0A            .str.split(',').str.get(1)%0A            .str.split('.').str.get(0)%0A            .str.strip()%0A            .map(title_map))%0A

Add New Feature: title