text_plain_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

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

sns.set_style('whitegrid')
plt.axis('equal')

# Categorical features
categorical_feats=['HomePlanet', 'CryoSleep', 'Destination', 'VIP']

# Plot categorical features
fig=plt.figure(figsize=(10,16))
for i, var_name in enumerate(categorical_feats):
    ax=fig.add_subplot(4,1,i+1)
    sns.countplot(data=traindf, x=var_name, axes=ax, hue='Transported')
    ax.set_title(var_name)
fig.tight_layout()  # Improves appearance a bit
plt.show()

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+'

plt.figure(figsize=(10, 4))
g = sns.countplot(data=traindf, x='Expenses_group', hue='Transported', order=['Expenses_0', 'Expenses_0-500', 'Expenses_500-1000', 'Expenses_1000-5000', 'Expenses_5000+'])
plt.title('Distribuição por grupo de gastos')
plt.show()