text_plain_output_1.png
import numpy as np

import os

import pandas as pd

data_dir = '../input'
os.listdir(f'{data_dir}')

train_df_raw = pd.read_csv(f'{data_dir}/train.csv', low_memory=False)
test_df_raw = pd.read_csv(f'{data_dir}/test.csv', low_memory=False)
train_df_raw.sample(10)

train_df_raw.describe(include='all').T

test_df_raw.describe(include='all').T

train_df = train_df_raw
test_df = test_df_raw
all_df = [train_df, test_df]

category_maps = {}
def Categorify(df: pd.DataFrame, cat_names):
    for cat_name in cat_names:
        uniques = df[cat_name].unique()
        category_maps[cat_name] = {i: uniques[i] for i in range(len(uniques))}
        df[cat_name] = [np.where(uniques == key)[0][0] for key in df[cat_name]]

cat_names = ['Sex', 'Embarked']
list(map(lambda df: Categorify(df, cat_names), all_df))
category_maps