text_plain_output_1.png
import matplotlib.pyplot as plt

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

import seaborn as sns

train_df = pd.read_csv('/kaggle/input/titanic/train.csv')
train_df.head().T

test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
test_df.head().T

train_df.head().T

train_df = train_df.drop(['PassengerId', 'Name', 'Ticket'], axis=1)
test_df = test_df.drop(['Name', 'Ticket'], axis=1)

train_df.isnull().sum()

test_df.isnull().sum()

# Embarked variable 

# 2 missing data in the train Data set .. So fill it with Mode. 

train_df["Embarked"].fillna(train_df["Embarked"].mode()[0], inplace=True)

# check to verify the null is gone in the embarked 

train_df.isnull().sum()

# Plot the embarked and survival relation

fig, (axis1,axis2,axis3) = plt.subplots(1,3,figsize=(15,5))

sns.countplot(x='Embarked', data=train_df , ax = axis1 )
sns.countplot(x='Survived', hue="Embarked", data=train_df, order=[1,0] , ax = axis2)

combined = train_df[["Embarked", "Survived"]].groupby(['Embarked'],as_index=False).mean()
sns.barplot(x='Embarked', y='Survived', data=combined,order=['S','C','Q'],ax=axis3)

# convert the category variable to numeric by dummy variable method

train_df1 = pd.get_dummies(train_df, prefix ='Embark', columns = ['Embarked'])
test_df1 = pd.get_dummies(test_df, prefix ='Embark', columns = ['Embarked'])

train_df1.head().T

test_df1['Fare'].fillna(test_df1['Fare'].median(), inplace=True)

#age 
# age has missing values in the train and test . replaced with the median 

test_df1["Age"].fillna(test_df1["Age"].median(), inplace=True)
train_df1["Age"].fillna(train_df1["Age"].median(), inplace=True)

# check if the NAN has removed with median
test_df1.isnull().sum()

# convert from float to int
train_df1['Age'] = train_df1['Age'].astype(int)
test_df1['Age']    = test_df1['Age'].astype(int)

#plot for aged and survived
# average survived passengers by age

fig, axis1 = plt.subplots(1,1,figsize=(18,4))
average_age = train_df1[["Age", "Survived"]].groupby(['Age'],as_index=False).mean()
sns.barplot(x='Age', y='Survived', data=average_age)

train_df1.drop('Cabin', axis=1, inplace=True)
test_df1.drop('Cabin', axis=1, inplace=True)

train_df1.head().T