3

Estoy intentando generar una gráfica con matplotlib a partir de un dataframe. Lo que deseo mostrar en la gráfica es una correlación entre la edad de las personas y el dinero que ganan y el que gastan. El problema que no logro que la gráfica sea entendible ni legible.

Intenté agrupar la información para que por ejemplo no se repitan las edades y sus respectivos valores, pero no lo he logrado.

Si pudieran ayudarme a generar correctamente la gráfica por favor. Gracias

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

import matplotlib.pyplot as plt
import pandas as pd

data = [{'Genre':1,'Age':19,'annualincome':15,'annualexpenses':39},
{'Genre':1,'Age':21,'annualincome':15,'annualexpenses':81},
{'Genre':0,'Age':20,'annualincome':16,'annualexpenses':6},
{'Genre':0,'Age':23,'annualincome':16,'annualexpenses':77},
{'Genre':0,'Age':31,'annualincome':17,'annualexpenses':40},
{'Genre':0,'Age':22,'annualincome':17,'annualexpenses':76},
{'Genre':0,'Age':35,'annualincome':18,'annualexpenses':6},
{'Genre':0,'Age':23,'annualincome':18,'annualexpenses':94},
{'Genre':1,'Age':64,'annualincome':19,'annualexpenses':3},
{'Genre':0,'Age':30,'annualincome':19,'annualexpenses':72},
{'Genre':1,'Age':67,'annualincome':19,'annualexpenses':14},
{'Genre':0,'Age':35,'annualincome':19,'annualexpenses':99},
{'Genre':0,'Age':58,'annualincome':20,'annualexpenses':15},
{'Genre':0,'Age':24,'annualincome':20,'annualexpenses':77},
{'Genre':1,'Age':37,'annualincome':20,'annualexpenses':13},
{'Genre':1,'Age':22,'annualincome':20,'annualexpenses':79},
{'Genre':0,'Age':35,'annualincome':21,'annualexpenses':35},
{'Genre':1,'Age':20,'annualincome':21,'annualexpenses':66},
{'Genre':1,'Age':52,'annualincome':23,'annualexpenses':29},
{'Genre':0,'Age':35,'annualincome':23,'annualexpenses':98},
{'Genre':1,'Age':35,'annualincome':24,'annualexpenses':35},
{'Genre':1,'Age':25,'annualincome':24,'annualexpenses':73},
{'Genre':0,'Age':46,'annualincome':25,'annualexpenses':5},
{'Genre':1,'Age':31,'annualincome':25,'annualexpenses':73},
{'Genre':0,'Age':54,'annualincome':28,'annualexpenses':14},
{'Genre':1,'Age':29,'annualincome':28,'annualexpenses':82},
{'Genre':0,'Age':45,'annualincome':28,'annualexpenses':32},
{'Genre':1,'Age':35,'annualincome':28,'annualexpenses':61},
{'Genre':0,'Age':40,'annualincome':29,'annualexpenses':31},
{'Genre':0,'Age':23,'annualincome':29,'annualexpenses':87},
{'Genre':1,'Age':60,'annualincome':30,'annualexpenses':4},
{'Genre':0,'Age':21,'annualincome':30,'annualexpenses':73},
{'Genre':1,'Age':53,'annualincome':33,'annualexpenses':4},
{'Genre':1,'Age':18,'annualincome':33,'annualexpenses':92},
{'Genre':0,'Age':49,'annualincome':33,'annualexpenses':14},
{'Genre':0,'Age':21,'annualincome':33,'annualexpenses':81},
{'Genre':0,'Age':42,'annualincome':34,'annualexpenses':17},
{'Genre':0,'Age':30,'annualincome':34,'annualexpenses':73},
{'Genre':0,'Age':36,'annualincome':37,'annualexpenses':26},
{'Genre':0,'Age':20,'annualincome':37,'annualexpenses':75},
{'Genre':0,'Age':65,'annualincome':38,'annualexpenses':35},
{'Genre':1,'Age':24,'annualincome':38,'annualexpenses':92},
{'Genre':1,'Age':48,'annualincome':39,'annualexpenses':36},
{'Genre':0,'Age':31,'annualincome':39,'annualexpenses':61},
{'Genre':0,'Age':49,'annualincome':39,'annualexpenses':28},
{'Genre':0,'Age':24,'annualincome':39,'annualexpenses':65},
{'Genre':0,'Age':50,'annualincome':40,'annualexpenses':55},
{'Genre':0,'Age':27,'annualincome':40,'annualexpenses':47},
{'Genre':0,'Age':29,'annualincome':40,'annualexpenses':42},
{'Genre':0,'Age':31,'annualincome':40,'annualexpenses':42},
{'Genre':0,'Age':49,'annualincome':42,'annualexpenses':52},
{'Genre':1,'Age':33,'annualincome':42,'annualexpenses':60},
{'Genre':0,'Age':31,'annualincome':43,'annualexpenses':54},
{'Genre':1,'Age':59,'annualincome':43,'annualexpenses':60},
{'Genre':0,'Age':50,'annualincome':43,'annualexpenses':45},
{'Genre':1,'Age':47,'annualincome':43,'annualexpenses':41},
{'Genre':0,'Age':51,'annualincome':44,'annualexpenses':50},
{'Genre':1,'Age':69,'annualincome':44,'annualexpenses':46},
{'Genre':0,'Age':27,'annualincome':46,'annualexpenses':51},
{'Genre':1,'Age':53,'annualincome':46,'annualexpenses':46},
{'Genre':1,'Age':70,'annualincome':46,'annualexpenses':56},
{'Genre':1,'Age':19,'annualincome':46,'annualexpenses':55},
{'Genre':0,'Age':67,'annualincome':47,'annualexpenses':52},
{'Genre':0,'Age':54,'annualincome':47,'annualexpenses':59},
{'Genre':1,'Age':63,'annualincome':48,'annualexpenses':51},
{'Genre':1,'Age':18,'annualincome':48,'annualexpenses':59},
{'Genre':0,'Age':43,'annualincome':48,'annualexpenses':50},
{'Genre':0,'Age':68,'annualincome':48,'annualexpenses':48},
{'Genre':1,'Age':19,'annualincome':48,'annualexpenses':59},
{'Genre':0,'Age':32,'annualincome':48,'annualexpenses':47},
{'Genre':1,'Age':70,'annualincome':49,'annualexpenses':55},
{'Genre':0,'Age':47,'annualincome':49,'annualexpenses':42},
{'Genre':0,'Age':60,'annualincome':50,'annualexpenses':49},
{'Genre':0,'Age':60,'annualincome':50,'annualexpenses':56},
{'Genre':1,'Age':59,'annualincome':54,'annualexpenses':47},
{'Genre':1,'Age':26,'annualincome':54,'annualexpenses':54},
{'Genre':0,'Age':45,'annualincome':54,'annualexpenses':53},
{'Genre':1,'Age':40,'annualincome':54,'annualexpenses':48},
{'Genre':0,'Age':23,'annualincome':54,'annualexpenses':52},
{'Genre':0,'Age':49,'annualincome':54,'annualexpenses':42},
{'Genre':1,'Age':57,'annualincome':54,'annualexpenses':51},
{'Genre':1,'Age':38,'annualincome':54,'annualexpenses':55},
{'Genre':1,'Age':67,'annualincome':54,'annualexpenses':41},
{'Genre':0,'Age':46,'annualincome':54,'annualexpenses':44},
{'Genre':0,'Age':21,'annualincome':54,'annualexpenses':57},
{'Genre':1,'Age':48,'annualincome':54,'annualexpenses':46},
{'Genre':0,'Age':55,'annualincome':57,'annualexpenses':58},
{'Genre':0,'Age':22,'annualincome':57,'annualexpenses':55},
{'Genre':0,'Age':34,'annualincome':58,'annualexpenses':60},
{'Genre':0,'Age':50,'annualincome':58,'annualexpenses':46},
{'Genre':0,'Age':68,'annualincome':59,'annualexpenses':55},
{'Genre':1,'Age':18,'annualincome':59,'annualexpenses':41},
{'Genre':1,'Age':48,'annualincome':60,'annualexpenses':49},
{'Genre':0,'Age':40,'annualincome':60,'annualexpenses':40},
{'Genre':0,'Age':32,'annualincome':60,'annualexpenses':42},
{'Genre':1,'Age':24,'annualincome':60,'annualexpenses':52},
{'Genre':0,'Age':47,'annualincome':60,'annualexpenses':47},
{'Genre':0,'Age':27,'annualincome':60,'annualexpenses':50},
{'Genre':1,'Age':48,'annualincome':61,'annualexpenses':42},
{'Genre':1,'Age':20,'annualincome':61,'annualexpenses':49},
{'Genre':0,'Age':23,'annualincome':62,'annualexpenses':41},
{'Genre':0,'Age':49,'annualincome':62,'annualexpenses':48},
{'Genre':1,'Age':67,'annualincome':62,'annualexpenses':59},
{'Genre':1,'Age':26,'annualincome':62,'annualexpenses':55},
{'Genre':1,'Age':49,'annualincome':62,'annualexpenses':56},
{'Genre':0,'Age':21,'annualincome':62,'annualexpenses':42},
{'Genre':0,'Age':66,'annualincome':63,'annualexpenses':50},
{'Genre':1,'Age':54,'annualincome':63,'annualexpenses':46},
{'Genre':1,'Age':68,'annualincome':63,'annualexpenses':43},
{'Genre':1,'Age':66,'annualincome':63,'annualexpenses':48},
{'Genre':1,'Age':65,'annualincome':63,'annualexpenses':52},
{'Genre':0,'Age':19,'annualincome':63,'annualexpenses':54},
{'Genre':0,'Age':38,'annualincome':64,'annualexpenses':42},
{'Genre':1,'Age':19,'annualincome':64,'annualexpenses':46},
{'Genre':0,'Age':18,'annualincome':65,'annualexpenses':48},
{'Genre':0,'Age':19,'annualincome':65,'annualexpenses':50},
{'Genre':0,'Age':63,'annualincome':65,'annualexpenses':43},
{'Genre':0,'Age':49,'annualincome':65,'annualexpenses':59},
{'Genre':0,'Age':51,'annualincome':67,'annualexpenses':43},
{'Genre':0,'Age':50,'annualincome':67,'annualexpenses':57},
{'Genre':1,'Age':27,'annualincome':67,'annualexpenses':56},
{'Genre':0,'Age':38,'annualincome':67,'annualexpenses':40},
{'Genre':0,'Age':40,'annualincome':69,'annualexpenses':58},
{'Genre':1,'Age':39,'annualincome':69,'annualexpenses':91},
{'Genre':0,'Age':23,'annualincome':70,'annualexpenses':29},
{'Genre':0,'Age':31,'annualincome':70,'annualexpenses':77},
{'Genre':1,'Age':43,'annualincome':71,'annualexpenses':35},
{'Genre':1,'Age':40,'annualincome':71,'annualexpenses':95},
{'Genre':1,'Age':59,'annualincome':71,'annualexpenses':11},
{'Genre':1,'Age':38,'annualincome':71,'annualexpenses':75},
{'Genre':1,'Age':47,'annualincome':71,'annualexpenses':9},
{'Genre':1,'Age':39,'annualincome':71,'annualexpenses':75},
{'Genre':0,'Age':25,'annualincome':72,'annualexpenses':34},
{'Genre':0,'Age':31,'annualincome':72,'annualexpenses':71},
{'Genre':1,'Age':20,'annualincome':73,'annualexpenses':5},
{'Genre':0,'Age':29,'annualincome':73,'annualexpenses':88},
{'Genre':0,'Age':44,'annualincome':73,'annualexpenses':7},
{'Genre':1,'Age':32,'annualincome':73,'annualexpenses':73},
{'Genre':1,'Age':19,'annualincome':74,'annualexpenses':10},
{'Genre':0,'Age':35,'annualincome':74,'annualexpenses':72},
{'Genre':0,'Age':57,'annualincome':75,'annualexpenses':5},
{'Genre':1,'Age':32,'annualincome':75,'annualexpenses':93},
{'Genre':0,'Age':28,'annualincome':76,'annualexpenses':40},
{'Genre':0,'Age':32,'annualincome':76,'annualexpenses':87},
{'Genre':1,'Age':25,'annualincome':77,'annualexpenses':12},
{'Genre':1,'Age':28,'annualincome':77,'annualexpenses':97},
{'Genre':1,'Age':48,'annualincome':77,'annualexpenses':36},
{'Genre':0,'Age':32,'annualincome':77,'annualexpenses':74},
{'Genre':0,'Age':34,'annualincome':78,'annualexpenses':22},
{'Genre':1,'Age':34,'annualincome':78,'annualexpenses':90},
{'Genre':1,'Age':43,'annualincome':78,'annualexpenses':17},
{'Genre':1,'Age':39,'annualincome':78,'annualexpenses':88},
{'Genre':0,'Age':44,'annualincome':78,'annualexpenses':20},
{'Genre':0,'Age':38,'annualincome':78,'annualexpenses':76},
{'Genre':0,'Age':47,'annualincome':78,'annualexpenses':16},
{'Genre':0,'Age':27,'annualincome':78,'annualexpenses':89},
{'Genre':1,'Age':37,'annualincome':78,'annualexpenses':1},
{'Genre':0,'Age':30,'annualincome':78,'annualexpenses':78},
{'Genre':1,'Age':34,'annualincome':78,'annualexpenses':1},
{'Genre':0,'Age':30,'annualincome':78,'annualexpenses':73},
{'Genre':0,'Age':56,'annualincome':79,'annualexpenses':35},
{'Genre':0,'Age':29,'annualincome':79,'annualexpenses':83},
{'Genre':1,'Age':19,'annualincome':81,'annualexpenses':5},
{'Genre':0,'Age':31,'annualincome':81,'annualexpenses':93},
{'Genre':1,'Age':50,'annualincome':85,'annualexpenses':26},
{'Genre':0,'Age':36,'annualincome':85,'annualexpenses':75},
{'Genre':1,'Age':42,'annualincome':86,'annualexpenses':20},
{'Genre':0,'Age':33,'annualincome':86,'annualexpenses':95},
{'Genre':0,'Age':36,'annualincome':87,'annualexpenses':27},
{'Genre':1,'Age':32,'annualincome':87,'annualexpenses':63},
{'Genre':1,'Age':40,'annualincome':87,'annualexpenses':13},
{'Genre':1,'Age':28,'annualincome':87,'annualexpenses':75},
{'Genre':1,'Age':36,'annualincome':87,'annualexpenses':10},
{'Genre':1,'Age':36,'annualincome':87,'annualexpenses':92},
{'Genre':0,'Age':52,'annualincome':88,'annualexpenses':13},
{'Genre':0,'Age':30,'annualincome':88,'annualexpenses':86},
{'Genre':1,'Age':58,'annualincome':88,'annualexpenses':15},
{'Genre':1,'Age':27,'annualincome':88,'annualexpenses':69},
{'Genre':1,'Age':59,'annualincome':93,'annualexpenses':14},
{'Genre':1,'Age':35,'annualincome':93,'annualexpenses':90},
{'Genre':0,'Age':37,'annualincome':97,'annualexpenses':32},
{'Genre':0,'Age':32,'annualincome':97,'annualexpenses':86},
{'Genre':1,'Age':46,'annualincome':98,'annualexpenses':15},
{'Genre':0,'Age':29,'annualincome':98,'annualexpenses':88},
{'Genre':0,'Age':41,'annualincome':99,'annualexpenses':39},
{'Genre':1,'Age':30,'annualincome':99,'annualexpenses':97},
{'Genre':0,'Age':54,'annualincome':101,'annualexpenses':24},
{'Genre':1,'Age':28,'annualincome':101,'annualexpenses':68},
{'Genre':0,'Age':41,'annualincome':103,'annualexpenses':17},
{'Genre':0,'Age':36,'annualincome':103,'annualexpenses':85},
{'Genre':0,'Age':34,'annualincome':103,'annualexpenses':23},
{'Genre':0,'Age':32,'annualincome':103,'annualexpenses':69},
{'Genre':1,'Age':33,'annualincome':113,'annualexpenses':8},
{'Genre':0,'Age':38,'annualincome':113,'annualexpenses':91},
{'Genre':0,'Age':47,'annualincome':120,'annualexpenses':16},
{'Genre':0,'Age':35,'annualincome':120,'annualexpenses':79},
{'Genre':0,'Age':45,'annualincome':126,'annualexpenses':28},
{'Genre':1,'Age':32,'annualincome':126,'annualexpenses':74},
{'Genre':1,'Age':32,'annualincome':137,'annualexpenses':18},
{'Genre':1,'Age':30,'annualincome':137,'annualexpenses':83}

]

df = pd.DataFrame.from_dict(data, orient='columns')

edades = df.Age
ingresos = df.annualincome
egresos = df.annualexpenses
x1 = np.arange(len(edades))  # the label locations
width = 0.35  # the width of the bars
fig, ax = plt.subplots()
rect1 = ax.bar(x1 - width/2, ingresos, width, label='annualincome')
rect2 = ax.bar(x1 + width/2, egresos, width, label='annualexpenses')
ax.set_ylabel('Scores')
ax.set_title('Scores by ingresos y egresos')
ax.set_xticks(x1)
ax.set_xticklabels(edades)
ax.legend()

def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')


autolabel(rect1)
autolabel(rect2)

fig.tight_layout()

plt.show()

grafica

2
  • 2
    Hola, ¿te interesa discernir entre géneros en la grafica o no? Si es afirmativo, necesitas 4 barras por edad ¿Quieres una barra para cada edad o podrías agrupar rangos de edades,por ejemplo 18-28, 28-38, etc?
    – FJSevilla
    Commented el 27 may. 2020 a las 6:47
  • Hola @FJSevilla, no me importa discernir entre generos. Por el momento solo debo tomar en cuenta la edad mostrar los ingresos con sus egresos. Y si la gráfica se ve bien con todas las edades está bien, ahora si la vuelve ilegible mejor que sea por rangos de edades. Muchas gracias Commented el 27 may. 2020 a las 7:07

2 respuestas 2

6
+50

Para mejorar tu visualización, el primer paso es crear un nuevo dataframe agrupando la media de ingresos y gastos por edad, para luego hacer la gráfica. De esta manera, se reducen los puntos y facilita el análisis. Para aplicar estas soluciones he partido de tu dataframe df:

def my_aggs(x):
    names = {
        'MeanAnnualIncome': x['annualincome'].mean(),
        'MeanAnnuaExpenses':  x['annualexpenses'].mean()}
    return pd.Series(names, index=['MeanAnnualIncome','MeanAnnuaExpenses'])

df1=df.groupby(['Age']).apply(my_aggs).reset_index()
df1.head()

introducir la descripción de la imagen aquí

edad=df1.Age
ingresos = df1.MeanAnnualIncome
gastos = df1.MeanAnnuaExpenses

Otro aspecto que creo puede mejorar tu visualización (al tener muchos puntos), es usar otro tipo de gráfico, p.e líneas en vez de barras (puedes elegir el que te parece más claro).


Ejemplo visualización 1: Por rangos de edad

plt.plot(edad, ingresos,label='Ingresos', linewidth=2)
plt.plot(edad,gastos,label='Gastos', linewidth=2)
plt.ylabel('Ingresos / Gastos')
plt.xlabel('Edad')
plt.legend()
plt.grid(True)

plt.show()

Por rangos de edad


Ejemplo visualización 2: Con todas las edades

countEdades=list(range(min(edad),min(edad)+len(edad)+2))

fig = plt.figure()
rect = (0,0, 2, 2)
axes = fig.add_axes(rect)

axes.plot(edad, ingresos,label='Ingresos', linewidth=3)
axes.plot(edad, gastos,label='Gastos', linewidth=3)
axes.set_ylim(0, 100)
axes.set_xticks(countEdades)
axes.legend()
axes.grid(True)
axes.set_title('Ingresos y Gastos por edad')
axes.set_xlabel("edades")
axes.set_ylabel("Ingresos / Gastos")

Incluyendo todas las edades

Cambiando la visualización 2 a barras:

fig = plt.figure()
rect = (0,0, 2, 2)
axes = fig.add_axes(rect)

axes.bar(edad, ingresos,label='Ingresos', linewidth=3)
axes.bar(edad, gastos,label='Gastos', linewidth=3)
axes.set_ylim(0, 100)
axes.set_xticks(countEdades)
axes.legend()
axes.grid(True)
axes.set_title('Ingresos y Gastos por edad')
axes.set_xlabel("edades")
axes.set_ylabel("Ingresos / Gastos")

introducir la descripción de la imagen aquí


Ejemplo visualización 3: Usando Seaborn podemos usar un scatterplot con líneas de regresión para representar la relación entre la edad de las personas y el dinero que ganan y el que gastan. En primer lugar, cambiamos la forma de nuestro dataframe df1, para unir en una sola columna los importes medio y creamos una columna adicional que indique si el tipo de importe es Ingreso o Gasto:

dfIngresos=df.groupby(['Age'])['annualincome'].agg(['mean']).reset_index()
dfIngresos['Type']='Ingreso'

dfGastos=df.groupby(['Age'])['annualexpenses'].agg(['mean']).reset_index()
dfGastos['Type']='Gasto'

df2=pd.concat([dfIngresos, dfGastos])
df2

Nueva forma de los datos en df2

import seaborn as sns
x=df2.Age
y=df2.mean

sns.lmplot('Age', 'mean', hue='Type',data=df2, fit_reg=True)

scatterplot con líneas de regresión

2
  • Hola @Eli-js muchas gracias por la respuesta, es impresionante lo que hiciste. Estoy muy agradecido. Solo me quedo una duda de como se interpreta el gráfico 3. Podríamos decir que conforme avanza la edad de una persona, sus gastos se reducen aunque se mantengan sus ingresos? Saludos. Commented el 1 jun. 2020 a las 6:25
  • 2
    Hola @raintrooper, lmplot muestra la recta de regresión y el intervalo de confianza. Esto da una idea de la relación entre las variables. A priori puedes hacer algunas apreciaciones (cuando la edad aumenta los gastos tienden a disminuir y los ingresos se reducen ligeramente), pero es evidente que los puntos no se ajustan a la recta (el error es alto), esto significa que la edad puede ser un factor importante para predecir los ingresos/gastos, pero seguramente existen otros factores que inciden. Para tener un modelo más ajustado, sería necesario incluir más variables a la ecuación.
    – EJS
    Commented el 1 jun. 2020 a las 18:13
0

para agrupar prueba asi:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

import matplotlib.pyplot as plt
import pandas as pd

data = [{'Genre':1,'Age':19,'annualincome':15,'annualexpenses':39},
{'Genre':1,'Age':21,'annualincome':15,'annualexpenses':81},
{'Genre':0,'Age':20,'annualincome':16,'annualexpenses':6},
{'Genre':0,'Age':23,'annualincome':16,'annualexpenses':77},
{'Genre':0,'Age':31,'annualincome':17,'annualexpenses':40},
{'Genre':0,'Age':22,'annualincome':17,'annualexpenses':76},
{'Genre':0,'Age':35,'annualincome':18,'annualexpenses':6},
{'Genre':0,'Age':23,'annualincome':18,'annualexpenses':94},
{'Genre':1,'Age':64,'annualincome':19,'annualexpenses':3},
{'Genre':0,'Age':30,'annualincome':19,'annualexpenses':72},
{'Genre':1,'Age':67,'annualincome':19,'annualexpenses':14},
{'Genre':0,'Age':35,'annualincome':19,'annualexpenses':99},
{'Genre':0,'Age':58,'annualincome':20,'annualexpenses':15},
{'Genre':0,'Age':24,'annualincome':20,'annualexpenses':77},
{'Genre':1,'Age':37,'annualincome':20,'annualexpenses':13},
{'Genre':1,'Age':22,'annualincome':20,'annualexpenses':79},
{'Genre':0,'Age':35,'annualincome':21,'annualexpenses':35},
{'Genre':1,'Age':20,'annualincome':21,'annualexpenses':66},
{'Genre':1,'Age':52,'annualincome':23,'annualexpenses':29},
{'Genre':0,'Age':35,'annualincome':23,'annualexpenses':98},
{'Genre':1,'Age':35,'annualincome':24,'annualexpenses':35},
{'Genre':1,'Age':25,'annualincome':24,'annualexpenses':73},
{'Genre':0,'Age':46,'annualincome':25,'annualexpenses':5},
{'Genre':1,'Age':31,'annualincome':25,'annualexpenses':73},
{'Genre':0,'Age':54,'annualincome':28,'annualexpenses':14},
{'Genre':1,'Age':29,'annualincome':28,'annualexpenses':82},
{'Genre':0,'Age':45,'annualincome':28,'annualexpenses':32},
{'Genre':1,'Age':35,'annualincome':28,'annualexpenses':61},

]

df = pd.DataFrame.from_dict(data, orient='columns')
grouped_gender = df.groupby("Genre")
grouped_gender.groups

for names, groups in grouped_gender:
    print(names)
    print(groups)



grouped_gender.aggregate(
    {
        "Age" : np.mean,
        "annualincome": np.sum,
        "annualexpenses" :np.std
    }
)
1
  • No creo que sea lo que el quiere. Commented el 2 jun. 2020 a las 19:55

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.