text_plain_output_1.png
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

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

model_cols = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Cabin', 'Embarked']

train_x = pd.get_dummies(train_df[model_cols], columns=['Pclass', 'Sex', 'SibSp', 'Cabin', 'Embarked'])
train_y = train_df['Survived']

test_df = pd.read_csv('../input/test.csv')
test_x = pd.get_dummies(test_df[model_cols], columns=['Pclass', 'Sex', 'SibSp', 'Cabin', 'Embarked'])
test_x['Age'].fillna(train_x['Age'].median(), inplace=True)
test_x['Fare'].fillna(train_x['Fare'].median(), inplace=True)

train_cols = train_x.columns
test_cols_all = test_x.columns
test_cols = [x for x in train_cols if x in test_cols_all]
test_xx = test_x[test_cols]
lc = len(train_x.columns)
j = 0
for i in range(lc):
    if train_cols[i] == test_cols[j]:
        j += 1
        continue
    else:
        test_xx.insert(i, train_cols[i], 0)

test_xx.columns[1:].append(pd.Index(['Age']))

def cmp(a, b):
    return (a > b) - (a < b)
cmp(test_xx.columns, train_x.columns)