text_plain_output_1.png
import numpy as np

import pandas as pd

traindf = pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')
testdf = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')
data_cleaner = [traindf, testdf]

traindf.shape

traindf.columns

traindf['Expenses_group'] = np.nan
traindf.loc[traindf['totalExpenses'] == 0, 'Expenses_group'] = 'Expenses_0'
traindf.loc[(traindf['totalExpenses'] > 0) & (traindf['totalExpenses'] <= 500), 'Expenses_group'] = 'Expenses_0-500'
traindf.loc[(traindf['totalExpenses'] > 500) & (traindf['totalExpenses'] <= 1000), 'Expenses_group'] = 'Expenses_500-1000'
traindf.loc[(traindf['totalExpenses'] > 1000) & (traindf['totalExpenses'] <= 5000), 'Expenses_group'] = 'Expenses_1000-5000'
traindf.loc[(traindf['totalExpenses'] > 5000) & (traindf['totalExpenses'] <= 500000000000), 'Expenses_group'] = 'Expenses_5000+'
testdf['Expenses_group'] = np.nan
testdf.loc[testdf['totalExpenses'] == 0, 'Expenses_group'] = 'Expenses_0'
testdf.loc[(testdf['totalExpenses'] > 0) & (testdf['totalExpenses'] <= 500), 'Expenses_group'] = 'Expenses_0-500'
testdf.loc[(testdf['totalExpenses'] > 500) & (testdf['totalExpenses'] <= 1000), 'Expenses_group'] = 'Expenses_500-1000'
testdf.loc[(testdf['totalExpenses'] > 1000) & (testdf['totalExpenses'] <= 5000), 'Expenses_group'] = 'Expenses_1000-5000'
testdf.loc[(testdf['totalExpenses'] > 5000) & (testdf['totalExpenses'] <= 500000000000), 'Expenses_group'] = 'Expenses_5000+'

traindf['Age_group'] = np.nan
traindf.loc[traindf['Age'] <= 12, 'Age_group'] = 'Age_0-12'
traindf.loc[(traindf['Age'] > 12) & (traindf['Age'] < 18), 'Age_group'] = 'Age_13-17'
traindf.loc[(traindf['Age'] >= 18) & (traindf['Age'] <= 25), 'Age_group'] = 'Age_18-25'
traindf.loc[(traindf['Age'] > 25) & (traindf['Age'] <= 30), 'Age_group'] = 'Age_26-30'
traindf.loc[(traindf['Age'] > 30) & (traindf['Age'] <= 50), 'Age_group'] = 'Age_31-50'
traindf.loc[traindf['Age'] > 50, 'Age_group'] = 'Age_51+'
testdf['Age_group'] = np.nan
testdf.loc[testdf['Age'] <= 12, 'Age_group'] = 'Age_0-12'
testdf.loc[(testdf['Age'] > 12) & (testdf['Age'] < 18), 'Age_group'] = 'Age_13-17'
testdf.loc[(testdf['Age'] >= 18) & (testdf['Age'] <= 25), 'Age_group'] = 'Age_18-25'
testdf.loc[(testdf['Age'] > 25) & (testdf['Age'] <= 30), 'Age_group'] = 'Age_26-30'
testdf.loc[(testdf['Age'] > 30) & (testdf['Age'] <= 50), 'Age_group'] = 'Age_31-50'
testdf.loc[testdf['Age'] > 50, 'Age_group'] = 'Age_51+'

traindf[['Age_group', 'Transported']].groupby(['Age_group'], as_index=False).mean().sort_values(by='Age_group', ascending=True)