1
resposta

Desafio: hora da prática

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/Gabriellemga/Python_Cursos_Alura/refs/heads/main/Data_viz_estilizando_tabelas/loja_livro_filmes.csv')
df.head()

df_custos = df.copy()
df_custos = df_custos[['pais','categoria', 'tipo_cliente', 'custo_envio']]
df_custos.columns = ['Custo por pais($)', 'Categoria do produto', 'Tipo de cliente', 'Custo de envio']
custo_pais = df_custos.pivot_table(index = ['Tipo de cliente', 'Categoria do produto'], columns = 'Custo por pais($)',
                                   values = 'Custo de envio', aggfunc = 'sum' )
custo_pais['Total($)'] = custo_pais.sum(axis = 1)
custo_pais

custos_pais_estilo = custo_pais.style.format('{:,.2f}')
tabela = {
    'selector':'td, th:not(index_name)',
    'props':'font-weight: normal; font-family : Times; text-align: center; background-color: white'
}

index = {
    'selector':'.index_name',
    'props': 'font-weight: bold ; font-family: Times; text-align: right; font-style: italic; color: #495057'
}

custos_pais_estilo.set_table_styles([tabela,index])

custos_pais_estilo.set_table_styles({
    ('B2B', 'BlueRay'): [{
        'selector': 'th, td',
        'props': 'border-top : 1px solid #181818'
    }],
       ('B2C', 'BlueRay'): [{
          'selector': 'th, td', 
          'props': 'border-top: 1px solid #181818'
          }],
    ('B2B', 'Livro'): [{
        'selector': 'th',
        'props': 'font-weight : bold'
    }],
    ('B2C', 'Livro'):[{
        'selector': 'th',
        'props': 'font-weight : bold'
    }]                 
}, overwrite = False, axis = 1)

custos_pais_estilo.set_table_styles({
    'Total($)': [{
        'selector': '.true',
        'props': 'font-weight: bold'
    }]
},overwrite=False,axis=0)

cores_coluna = pd.DataFrame(['false','false','false','true','false','false','false','true'],index= custo_pais['Total($)'].index,
                            columns = ['Total($)'])

custos_pais_estilo.set_td_classes(cores_coluna)
1 resposta

Muito bem, Marcia!