text_html_output_1.png
import pandas as pd

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

def wrangle(df):
    df['Male'] = df['Sex'].map(lambda x: x == 'male')
    df['Pronoun'] = df['Name'].map(lambda x: x.split(', ')[1].split('.')[0])
    df['Prefix'] = df['Ticket'].map(lambda x: x.split(' ')[0].split('/')[0].replace('.', '') if ' ' in x else '')
    df['Letter'] = df['Cabin'].fillna('00').map(lambda x: x[0])
    df['Family'] = df['SibSp'] + df['Parch']
    df = df.drop(['PassengerId', 'Name', 'Ticket', 'Sex', 'Cabin'], axis=1)
    return df

new_train = wrangle(train)

def aggregate(df, cols):
    by_cols = df.groupby(cols).agg({'Survived': [('Total', 'count'), ('Survived', 'sum')]})
    by_cols.columns = by_cols.columns.droplevel()
    by_cols['Died'] = by_cols['Total'] - by_cols['Survived']
    by_cols['Survive Rate'] = 100 * by_cols['Survived'] / by_cols['Total']
    return by_cols.sort_values('Total', ascending=False)

aggregate(new_train, ['Male'])