text_plain_output_1.png
import pandas as pd

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

categorical = train.dtypes[train.dtypes == 'object'].index

train.isnull().sum()

train['Initial'] = 0
for i in train:
    train['Initial'] = train.Name.str.extract('([A-Za-z]+)\\.')
test['Initial'] = 0
for i in test:
    test['Initial'] = train.Name.str.extract('([A-Za-z]+)\\.')

train.groupby('Initial')['Age'].mean()

train.loc[train.Age.isnull() & (train.Initial == 'Mr'), 'Age'] = 33
train.loc[train.Age.isnull() & (train.Initial == 'Mrs'), 'Age'] = 36
train.loc[train.Age.isnull() & (train.Initial == 'Master'), 'Age'] = 5
train.loc[train.Age.isnull() & (train.Initial == 'Miss'), 'Age'] = 22
train.loc[train.Age.isnull() & (train.Initial == 'Other'), 'Age'] = 46
test.loc[test.Age.isnull() & (test.Initial == 'Mr'), 'Age'] = 33
test.loc[test.Age.isnull() & (test.Initial == 'Mrs'), 'Age'] = 36
test.loc[test.Age.isnull() & (test.Initial == 'Master'), 'Age'] = 5
test.loc[test.Age.isnull() & (test.Initial == 'Miss'), 'Age'] = 22
test.loc[test.Age.isnull() & (test.Initial == 'Other'), 'Age'] = 46

bins = (0, 5, 12, 18, 25, 35, 60, 120)
group_names = ['Baby', 'Child', 'Teenager', 'Student', 'Young Adult', 'Adult', 'Senior']
categories = pd.cut(train['Age'], bins, labels=group_names)
train['Age'] = categories

bins = (0, 5, 12, 18, 25, 35, 60, 120)
group_names = ['Baby', 'Child', 'Teenager', 'Student', 'Young Adult', 'Adult', 'Senior']
categories = pd.cut(test['Age'], bins, labels=group_names)
test['Age'] = categories

group_names = ['1Q', '2Q', '3Q', '4Q']
quartiles = pd.qcut(train['Fare'], 4, labels=group_names)

train['Fare'] = quartiles
group_names = ['1Q', '2Q', '3Q', '4Q']
quartiles_test = pd.qcut(test['Fare'], 4, labels=group_names)
test['Fare'] = quartiles

train = train.drop(['Ticket', 'Name'], axis=1)
test = test.drop(['Ticket', 'Name'], axis=1)

pd.crosstab(index=train['Embarked'], columns='count')