Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Groupby the loan id and calculate the max of the months balance
- max_months_balance = bureau_balance.groupby('SK_ID_BUREAU',
- as_index = False)['MONTHS_BALANCE'].agg(['max'])
- # Rename the column
- max_months_balance = max_months_balance.rename(columns = {'max': 'max_months_balance'})
- # Need the client id in the dataframe
- max_months_balance = max_months_balance.merge(bureau_balance[['SK_ID_CURR', 'SK_ID_BUREAU']],
- on = 'SK_ID_BUREAU', how = 'left')
- # Groupby the client id and calculate the mean of the max
- average_loan_max_months_balance = max_months_balance.groupby('SK_ID_CURR'
- )['max_months_balance'].agg(['mean']).reset_index()
- # Rename the column
- average_loan_max_months_balance = average_loan_max_months_balance.rename(
- columns = {'mean': 'average_of_max_months_balance'})
- # Merge with the parent dataframe
- app = app.merge(average_loan_max_months_balance[['SK_ID_CURR', 'average_of_max_months_balance']],
- on = 'SK_ID_CURR', how = 'left')
Add Comment
Please, Sign In to add comment