text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png image_output_1.png
import pandas as pd

train_data = pd.read_csv('../input/titanic/train.csv')
test_data = pd.read_csv('../input/titanic/test.csv')

train_data = train_data.drop(['Cabin', 'Ticket', 'PassengerId'], axis=1)
test_data = test_data.drop(['Cabin', 'Ticket'], axis=1)

combine = [train_data, test_data]

for dataset in combine:
    dataset['Title'] = dataset.Name.str.extract(' ([A-Za-z]+)\\.', expand=False)
(pd.crosstab(train_data['Title'], train_data['Sex']), pd.crosstab(test_data['Title'], test_data['Sex']))

for dataset in combine:
    dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess', 'Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
    dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
    dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
    dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
pd.crosstab(train_data['Title'], train_data['Sex'])

train_data = train_data.drop(['Name'], axis=1)
test_data = test_data.drop(['Name'], axis=1)
combine = [train_data, test_data]

train_data = pd.concat([train_data, pd.get_dummies(train_data['Sex'])], axis=1)
test_data = pd.concat([test_data, pd.get_dummies(test_data['Sex'])], axis=1)
train_data