I have a huge dataframe in pandas that has following format:
period from_ to_ value
2020-07 Jonny Karl 15.00
2020-08 Matt Jonny 5.00
2020-08 Matt Karl 5.00
2020-08 Matt Karl 10.00
2020-08 Jonny Matt 10.00
Where I have values of one person need to pay to another person by a period of the year. The name of those persons repeat themselves over the dataset.
So I want to see per period of year the values one person owes to another person. For that, I can simply use:
sum_df = df.groupby([ "period", "from_", "to_"]).agg({"value": 'sum'})
But is here that lies my question, because I'm trying to figure out a performative way to do this aggregating function where it could "identify":
if person A owes 5$ to person B. and person B owe 10$ to person A. It should return that person B owe for that period 5$ to person A. Resulting in the following dataframe:
period from_ to_ value
2020-07 Jonny Karl 15.00
2020-08 Matt Karl 15.00
2020-08 Jonny Matt 5.00
Someone could give me a direction in which I could follow to reach that?