I have the following data:
There are six customers who took out loans multiple times offered by Agents. Agents serve multiple customers. For example Agent 306 provided loans to Customer 1 and 2. Similarly, Agent 309 provided loans to Customer 5 and 6. I would like to aggregate the amount of loans each customer took and get something like in the table below. It is important I see the gender of the customer and agent after aggregation.
I tried the following code:
import pandas as pd
data = {'Customer_ID': [1, 1, 1, 2, 2, 3, 4, 4, 5, 5, 5, 5, 6, 6],
'Loan': [200, 250, 300, 400, 300, 500, 150, 150, 400, 250, 150, 300, 200, 200],
'CustomerGender': ['M', 'M', 'M', 'F', 'F', 'M', 'M', 'M', 'F', 'F', 'F', 'F', 'F', 'F'],
'Agent_ID': [306, 306, 306, 306, 306, 307, 308, 308, 309, 309, 309, 309, 309, 309],
'AgentGender': ['F', 'F', 'F', 'M', 'M','M', 'M', 'M', 'F', 'F', 'F', 'F', 'F', 'F']}
# transform to dataframe
data = pd.DataFrame(data)
# declare the two gender columns categorical
data['AgentGender']=data['AgentGender'].astype('category')
data['CustomerGender']=data['CustomerGender'].astype('category')
# aggregate the data by Customer_ID to see the total amount of loan each customer took.
data.groupby(data['Customer_ID']).sum()
What I get is the following:
How can I aggregate by Customer_ID and be able to see the gender of customers and agents please? Many thanks.


