1

Can someone please explain what is actually going on in aggfunc here -

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

Thank you

3
  • 2
    It works the same as in any function... Although that is not the built-in map function, that is a method being accessed on the argument to the lambda function Commented Feb 1, 2018 at 1:05
  • @juanpa.arrivillaga Looks like pandas.Series.map. Commented Feb 1, 2018 at 1:10
  • I was confusing this with built-in map, this is actually pandas.Series.map. Thank you @Christian Dean Commented Feb 1, 2018 at 2:52

1 Answer 1

1

Below example should illustrate what's happening. The Loan_Status values are aggregated by Credit_History according to the logic "add up number of Y's and divide by total number of observations".

import pandas as pd

df = pd.DataFrame([['Y', 'A'], ['N', 'B'], ['Y', 'C'], ['N', 'A'], ['Y', 'C']],
                  columns=['Loan_Status', 'Credit_History'])

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

#                 Loan_Status
# Credit_History             
# A                       0.5
# B                       0.0
# C                       1.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.