text_plain_output_1.png
import numpy as np

import pandas as pd

import re

import pandas as pd
import re
import numpy as np
train = pd.read_csv('/kaggle/input/titanic/train.csv')
test = pd.read_csv('/kaggle/input/titanic/test.csv')

suv = pd.read_csv('/kaggle/input/titanicscraper/src/kaggle/titanic/surv.csv')
vic = pd.read_csv('/kaggle/input/titanicscraper/src/kaggle/titanic/vict.csv')

suv['survived'] = 1
vic['survived'] = 0
ground_truth = pd.concat([suv, vic])
ground_truth['fsname'] = [re.search('^(.*?)( |$)', item).group(1) for item in ground_truth['given name']]

tmp_f = [item.encode('ascii', 'ignore').decode('ascii') for item in ground_truth['family name']]
non_ascii = [True if x != y else False for x, y in zip(tmp_f, ground_truth['family name'])]
ground_truth['uni_f'] = non_ascii
pd.value_counts(non_ascii)

tmp_fs = [item.encode('ascii', 'ignore').decode('ascii') for item in ground_truth['fsname']]
non_ascii_ = [True if x != y else False for x, y in zip(tmp_fs, ground_truth['fsname'])]
ground_truth['uni_g'] = non_ascii_
pd.value_counts(non_ascii_)

ground_truth.set_index(np.arange(0, ground_truth.shape[0]), inplace=True)

for i, item in ground_truth.iterrows():
    dash = re.search('-', item['alt name'])
    if item.uni_f | item.uni_g | bool(dash):
        ground_truth.at[i, 'family name'] = item['alt name'].split('-')[-1].upper()
        ground_truth.at[i, 'fsname'] = item['alt name'].split('-')[0].capitalize()

train['fname'] = [re.search('^(.*?), ', item).group(1) for item in train.Name]
train['prefix'] = [re.search('^.*?, (.*?)\\. ', item).group(1) for item in train.Name]
train['gname'] = [re.search('^.*?, .*?\\. (.*)', item).group(1) for item in train.Name]

tmp = [re.search('^.*?, .*?\\. ([^ ]*?)( |$)', item).group(1) for item in train.Name]
tmp2 = [re.search('\\((.*?)( |\\)|$)', item).group(1) if item.startswith('(') else item for item in tmp]
tmp3 = [z.group(1) if y == 'Mrs' and (z := re.search('^.*?\\((.*?)( |\\))', x)) is not None else w for x, y, w in zip(train.gname, train.prefix, tmp2)]
train['fsname'] = tmp3
train['fname'] = [item.split('-')[-1] if bool(re.search('-', item)) else item for item in train['fname']]
train['fname'] = [item.split(' ')[-1] if bool(re.search(' ', item)) else item for item in train['fname']]
train['fname'] = [item.replace("'", '') if bool(re.search("'", item)) else item for item in train['fname']]