How to map values using 3 columns?


#1

Using pivot table, I am only able to find out Male who were married and irrespective of whether loan approved or not.
I want to find out Male who were married and were given Loan Status as ‘Yes’.

Snippet in Python -

df.pivot_table(index=[‘Credit_History’],values=‘Loan_Status’,aggfunc=lambda x:x.map({‘Y’:1,‘N’:0}).mean())

I want to find using this code but 3 columns instead of 2.


#2

Hey , to find “Male who were married and were given Loan Status as ‘Yes’” you can write all the three condition in your df.loc[…] . The following worked for me:

df.loc[(df["Gender"] == "Male") & (df["Married"] == "Yes") & (df["Loan_Status"] == "Y") ]

#3

Thans for your answer. But it works like a Filter in Excel. Even i end up using with a count attribute still it shows all the columns count.
I want it to displayed something like this but with different columns :

     Loan_Status

Credit_History
0.0 0.078652
1.0 0.795789

How to use using pivot_table?


#4

Hey @ASHISH_17,
You may try the following, which will get you the mean of Loan Status for all possible combinations of the 3 columns :

pivot = df.pivot_table(index=[‘Gender’,‘Married’,‘Credit_History’],values=‘Loan_Status’,aggfunc=lambda x:x.map({‘Y’:1,‘N’:0}).mean())

Hope this helps :slight_smile:


#5

Hello,I am trying to use the above code you just mentioned but I am getting an error.
KeyError:'Loan_Status’
raise KeyError(i)


#6

Even I’m facing the same error!!


#7

@chakradharbandlamoor
Just check that your dataframe df contains the column ‘Loan_Status’.


#8

yeah, you are right. Loan_status column is missing in my data set. Thanks @shubham.jain