text_html_output_1.png
import numpy as np

import pandas as pd

x = pd.read_csv('../input/train.csv')
x_2 = pd.read_csv('../input/train.csv')
y = pd.read_csv('../input/test.csv')
toPredict = x.pop('Survived')
data = pd.concat([x, y])

newage = data[['Age', 'Pclass', 'Sex']].dropna()
print('Pclass 1 F = ' + str(np.median(newage.query('Pclass == 1 and Sex == "female"')['Age'])))
print('Pclass 2 F = ' + str(np.median(newage.query('Pclass == 2 and Sex == "female"')['Age'])))
print('Pclass 3 F = ' + str(np.median(newage.query('Pclass == 3 and Sex == "female"')['Age'])))
print('Pclass 1 M = ' + str(np.median(newage.query('Pclass == 1 and Sex == "male"')['Age'])))
print('Pclass 2 M = ' + str(np.median(newage.query('Pclass == 2 and Sex == "male"')['Age'])))
print('Pclass 3 M = ' + str(np.median(newage.query('Pclass == 3 and Sex == "male"')['Age'])))
data1 = data.query('Pclass == 1 and Sex == "female"')
data1['Age'] = data1['Age'].fillna(36)
data2 = data.query('Pclass == 2 and Sex == "female"')
data2['Age'] = data2['Age'].fillna(28)
data3 = data.query('Pclass == 3 and Sex == "female"')
data3['Age'] = data3['Age'].fillna(22)
data4 = data.query('Pclass == 1 and Sex == "male"')
data4['Age'] = data4['Age'].fillna(42)
data5 = data.query('Pclass == 2 and Sex == "male"')
data5['Age'] = data5['Age'].fillna(29.5)
data6 = data.query('Pclass == 3 and Sex == "male"')
data6['Age'] = data6['Age'].fillna(25)
data = pd.concat([data1, data2, data3, data4, data5, data6])
data = data.sort('PassengerId')