How to have if statements in iloc

Viewed 37

I was thus to come up with a solution for The column "Loan_Term" which measures the duration of the loan applied for, in months.

Applicants asking for loans with loan terms less than 120 months are given a rating of "Short" under loan tenure. Those with loan terms at least 120 months, but less than 300 months will be rated as "Medium". Applicants asking for loans with loan terms of 300 or more months, will be rated as "Long".

Write a function that takes an applicant's numerical value of months of Loan_Term as an input parameter and returns the respective customer's rating. Create a new attribute "Loan_Tenure" for every applicant in df_loans.

Display df_loans with only the "Loan_Term" and "Loan_Tenure" attributes.

My code is as follows df_loans =df df_loans.loc[(df_loans.Loan_Term < 120 return "Short" ) | (df_loans.Loan_Term > 120 & < 300 return "Medium") | (df_loans.Loan_Term > 300 return "Long")]. It is wrong and I was wondering is there a way for it to only display this criterion in the table through loc or must i use something else.

1 Answers

for this you can use numpy's select function

df['Loan_Tenure'] = np.select([df_loans.Loan_Term <= 120 ,(df_loans.Loan_Term>120)&(df_loans.Loan_Term<300),(df_loans.Loan_Term >= 300)],['Short','Medium','Long'])
Related