I have a dataset name data. I would like to calculate the Lower and Upper value for multiple numeric variables: Loan, Amount, Value, LTV, UR. Instead of computing them one by one, how can I automate the following python code?
#Loan
Q1= data['LOAN'].quantile(q=0.25)
Q3= data['LOAN'].quantile(q=0.75)
IQR= Q3 - Q1
Lower = (Q1 - 1.5*IQR)
Upper = (Q3 1.5*IQR)
print('Loan')
print(Lower)
print(Upper)
CodePudding user response:
I'd define an array with the colum names, then i'll cycle all the columns calling a function that calculates the values:
names =['LOAN', 'AMOUNT','VALUE','LUV','UR']
for column in data[names]:
x,y=calculateLowerUpper(column)
print (column)
print(x)
print(y)
def calculateLowerUpper(column):
q1= data['LOAN'].quantile(q=0.25)
q3= data['LOAN'].quantile(q=0.75)
iqr= q3 - q1
lower = (q1 - 1.5*iqr)
upper = (q3 1.5*iqr)
return lower,upper