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

train = pd.read_csv('../input/titanic/train.csv')
test = pd.read_csv('../input/titanic/test.csv')
all_data = [train, test]
submition_id = test['PassengerId']

def check_missing(data, dtype='object'):
    if dtype == 'object':
        nulls = data.select_dtypes(include='object')
    else:
        nulls = data.select_dtypes(exclude='object')
    nulls = nulls.isnull().sum()
    nulls = nulls.drop(nulls[nulls == 0].index).sort_values(ascending=False)
    return pd.DataFrame(nulls)
    '\n    this function detecting the missing values by the type\n    and return data frame contains two columns \n    feature name : how many NAN\n    ----------\n    parameters\n    ----------\n    data : which data frame you need to check it\n    \n    dtype : do you looking for numerical featuers or categorical ? \n    \n    '

def lower_case_features(data):
    data.columns = [col.lower() for col in list(data)]
    '\n    simple function to return evry single column name in lowercase\n    list(data) ==> contains every column title in the data frame \n    \n    '
lower_case_features(train)
lower_case_features(test)

for dataset in [train, test]:
    dataset['agecat'] = pd.cut(dataset['age'], bins=5, labels=['0-16', '16-32', '32-48', '48-64', '64-85'], include_lowest=True)
pd.DataFrame(train[['agecat', 'survived']].value_counts())

pd.DataFrame(train.embarked.value_counts())

for d in all_data:
    d = d.drop(['passengerid', 'cabin', 'ticket'], axis=1, inplace=True)

def get_title(df):
    sliced_name = df['name'].str.split(expand=True)
    sliced_name[1] = sliced_name[1].str.replace('.', '', regex=True)
    df['title'] = sliced_name[1]
    df = df.drop(['name'], axis=1, inplace=True)
    '\n    this function split the name feature gets every single value between comma \n    and append it to a feature in a pandas data frame\n    \n    ----------\n    parameters\n    ----------\n    just your dataframe name\n    \n    -------\n    returns\n    -------\n    \n    your data frame with a new feature called title and without name feature\n    \n    '

def get_others(df):
    keep = ['Mr', 'Miss', 'Mrs', 'Master']
    titles = list(df['title'].values)
    others = [i for i in titles if i not in keep]
    df['title'] = df['title'].replace(others, 'other')
    return df
    "\n    \n    this function takes any value except ('Mr', 'Miss', 'Mrs', 'Master')\n    and append it to a list and replace the values in the data frame with other\n    \n    ----------\n    parameters\n    ----------\n    just the data set\n    \n    -------\n    returns\n    -------\n    the data frame with title feature with \n    "

for DF in [train, test]:
    get_others(DF)
print('now title values is')
test['title'].value_counts()