# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Tablas de Pivotaje

import numpy as np
import pandas as pd
import seaborn as sb # para cargar datos del titanic

titanic = sb.load_dataset("titanic")
titanic.head()

titanic.groupby('sex').mean() # agrupacions por sexo y la media

titanic.groupby('sex')[['survived']].mean()# por genero la media que sobrevivio

titanic.groupby(['sex', 'pclass'])['survived'].aggregate('mean')# agrupo por 'sex', 'pclass' de vivientes saca la media  

titanic.groupby(['sex', 'pclass'])['survived'].aggregate('mean').unstack()# los desapila

# ## Funcio de Pivotage

titanic.pivot_table('survived', index='sex', columns='pclass') # calcula las estadistic de 'survived' en las filas 'sex' y columnas 'pclass'

# ## Funcion Cut

age = pd.cut(titanic['age'], bins=[0,18,25, 35, 50, 65, 100])# calcula por la franja de Edad
age1 = pd.cut(titanic['age'], bins=[0,18,25, 35, 50, 65, 100], include_lowest=True, precision=1, right=False)
print("age:",age)
print("age1:",age1)

titanic.pivot_table('survived', index=['sex', age1], columns='class')# con las filas la combinacions genero y edad

# ### Dividir la info en cuantiles  "pd.qcut"

fare = pd.qcut(titanic['fare'],q=4, labels= ["Q1", "Q2","Q3","Q4"])
fare

titanic.pivot_table('survived', index=['sex', age], columns=[fare, 'pclass'])

# ## DataFrame.pivot_table(data, values=None, index=None, columns=None,                       aggfunc="mean", fill_value=None, margins=False,                       dropna=True, margins_name = "All")

titanic.pivot_table(index = "sex", columns="class", 
                   aggfunc = {"survived":sum, "fare":"mean"})

# ### uso de marginales

titanic.pivot_table('survived', index="sex", columns="class", margins=True, margins_name="Total")



StarcoderdataJupyterScriptsDedupFiltered