text_plain_output_1.png image_output_1.png
import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

df = pd.read_csv('/kaggle/input/dataviz-facens-20182-ex3/BlackFriday.csv', delimiter=',')

fig, ax = plt.subplots(figsize=(18,9))
plt.title('Valor Gasto por Faixa Etária', fontsize=14, fontweight = 'bold')

frame = plt.gca()
frame.spines['right'].set_visible(False)
frame.spines['top'].set_visible(False) 

sns.violinplot(ax = ax, x="Age", y="Purchase", data=df[['Age','Purchase']].sort_values(by=['Age']),  palette="RdBu_r")

g = sns.catplot(x="Purchase", y="Gender", col="Age",
                data=df.sort_values(by=['Age']), col_wrap=1,
                orient="h", height=3, aspect=3, palette="Set3",
                kind="violin", dodge=True, bw=.2)

import numpy as np

total = pd.DataFrame(df.Product_ID.value_counts()).head(9)

x = total.index.unique()
y = total.Product_ID

width = 0.8 

fig, ax = plt.subplots()

ax.barh(x, y, width, color = '#AAAAAA')

frame = plt.gca()
frame.spines['right'].set_visible(False)
frame.spines['top'].set_visible(False) 

for i, v in enumerate(y):
    ax.text(v/2 , i , str(v), color='#111111', fontweight='bold', verticalalignment='center')

ax.invert_yaxis()

fig.set_size_inches(15, 8) 
plt.title('Top 9 Produtos mais Comprados', fontsize=14, fontweight = 'bold')

plt.show()

occupation_order = list(df['Occupation'].value_counts().head(5).index)

df_target = df[df['Occupation'].isin(occupation_order)].sort_values(by='Age')

plt.figure(figsize=(20,10))
g = sns.boxplot(x="Occupation", y="Purchase", hue="Age", data=df_target)

plt.title('Valores gastos por faixa etária associados às 5 ocupações mais frequentes\n', fontsize=16)
plt.xlabel('Ocupação')
plt.ylabel('Valor gasto')
plt.legend(loc=1, title='Idade')
plt.ylim(0, 35000)

plt.show() 

total = df[df.Purchase > 9000].groupby(['Marital_Status', 'Occupation']).Purchase.sum().reset_index()
labels = total.Occupation.unique().astype(str)
casado = total[total.Marital_Status == 1].reset_index().Purchase / total.groupby('Occupation').sum().reset_index().Purchase
solteiro = total[total.Marital_Status == 0].reset_index().Purchase / total.groupby('Occupation').sum().reset_index().Purchase
width = 0.8
fig, ax = plt.subplots()
ax.bar(labels, casado, width, label='Casado', color='#7473aa')
ax.bar(labels, solteiro, width, label='Solteiro', bottom=casado, color='#f5ac9f')
frame = plt.gca()
frame.spines['right'].set_visible(False)
frame.spines['top'].set_visible(False)
ax.legend()
fig.set_size_inches(15, 10)
plt.title('Valor Gasto por Ocupação x Estado Civil', fontsize=14, fontweight='bold')
plt.show()