I'm making a function to get the IQR of a list of columns. But it returns me the following error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
The strange is that when I want to get the IQR of the first column of the list it works perfectly, but when I try other index in the list of columns it doesn't work. I have tried the methods bool(), all(), any(), item() and empty but it doesn't works in a list. Here's the code I have wrotten (All of the data doesn't exist, they are fictional characters):
import pandas as pd
import sklearn
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
ohe_encoder = OneHotEncoder()
diccionario_datos = {'Nombre': ['Eduardo', 'Ana', 'Alejandro', 'Álvaro', 'Aitana', 'María', 'Sofía', 'Antonio', 'Fernando', 'Laura'],
'Grupo Sanguíneo': ['B+', '0+', 'AB+', '0+', 'A+', 'A+', 'B-', 'A+', 'AB+', '0+'],
'Edad': [40, 35, 37, 24, 48, 53, 67, 25, 38, 21],
'Peso': [70, 69, 74, 70, 72, 67, 65, 74, 75, 70],
'Ritmo Cardíaco': [70, 60, 68, 50, 68, 87, 110, None, 77, 69],
'Presión sistólica': [129, 133, 125, 110, None, 130, 155, 126, 131, 127],
'Presión diastólica': [None, 86, 82, 70, 82, 84, 100, 89, 90, 87]}
datos = pd.DataFrame(diccionario_datos)
ohe_gruposanguineo = pd.DataFrame(ohe_encoder.fit_transform(datos[['Grupo Sanguíneo']]).toarray())
datos = datos.drop('Grupo Sanguíneo', axis=1)
datos = pd.concat([datos, ohe_gruposanguineo], axis=1)
quartiles = [datos['Edad'].quantile([0.25, 0.75]),
datos['Peso'].quantile([0.25, 0.75]),
datos['Ritmo Cardíaco'].quantile([0.25, 0.75]),
datos['Presión sistólica'].quantile([0.25, 0.75]),
datos['Presión diastólica'].quantile([0.25, 0.75])]
def IQR (datos, columna):
for caracteristica in quartiles:
if (columna - 1) == quartiles.index(caracteristica):
return(caracteristica[0.75] - caracteristica[0.25])
elif columna - 1 > len(quartiles) or columna - 1 <= -1:
return('Error: índice no contemplado en la longitud de la lista')
else:
continue
IQR(quartiles, 2)
Thank you for the help.
Based on your comment you can change your
IQR()function to: