text_plain_output_1.png
import pandas as pd

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

raw_data = pd.read_csv('/kaggle/input/titanic/train.csv')
raw_test = pd.read_csv('/kaggle/input/titanic/test.csv')

train_copy = raw_data.copy()
train_copy.set_index('PassengerId', inplace=True, drop=True)
test_copy = raw_test.copy()
test_copy.set_index('PassengerId', inplace=True, drop=True)

train_copy.isnull().sum()

test_data = test_copy.copy()
train_data = train_copy.copy()

train_data['modified_name'] = train_data.Name.str.split(',', expand=True)[1]
train_data['prefix'] = train_data['modified_name'].str.split('.', expand=True)[0]

test_data['modified_name'] = test_data.Name.str.split(',', expand=True)[1]
test_data['prefix'] = test_data['modified_name'].str.split('.', expand=True)[0]

fill = train_data[train_data.Age.isnull()]['prefix'].unique()
criteria = train_data[train_data['prefix'].isin(fill)]
train_age_summary = criteria.groupby(['prefix', 'Pclass'])['Age'].agg(['mean', 'count'])
train_age_summary

fillt = test_data[test_data.Age.isnull()]['prefix'].unique()
criteria1 = test_data[test_data['prefix'].isin(fill)]
test_age_summary = criteria.groupby(['prefix', 'Pclass'])['Age'].agg(['mean', 'count'])
test_age_summary