Guest User

Untitled

a guest
Jun 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # Groupby the loan id and calculate the max of the months balance
  2. max_months_balance = bureau_balance.groupby('SK_ID_BUREAU',
  3. as_index = False)['MONTHS_BALANCE'].agg(['max'])
  4.  
  5. # Rename the column
  6. max_months_balance = max_months_balance.rename(columns = {'max': 'max_months_balance'})
  7.  
  8. # Need the client id in the dataframe
  9. max_months_balance = max_months_balance.merge(bureau_balance[['SK_ID_CURR', 'SK_ID_BUREAU']],
  10. on = 'SK_ID_BUREAU', how = 'left')
  11.  
  12. # Groupby the client id and calculate the mean of the max
  13. average_loan_max_months_balance = max_months_balance.groupby('SK_ID_CURR'
  14. )['max_months_balance'].agg(['mean']).reset_index()
  15.  
  16. # Rename the column
  17. average_loan_max_months_balance = average_loan_max_months_balance.rename(
  18. columns = {'mean': 'average_of_max_months_balance'})
  19.  
  20. # Merge with the parent dataframe
  21. app = app.merge(average_loan_max_months_balance[['SK_ID_CURR', 'average_of_max_months_balance']],
  22. on = 'SK_ID_CURR', how = 'left')
Add Comment
Please, Sign In to add comment