I'm trying to dynamically build a format in which I want to display number of deposits compared to withdrawals in a timeline chart. Whenever a deposit is done, the graph will go up, and when a withdrawal is done the graph goes down.
This is how far I've gotten:
df.head()
name Deposits Withdrawals
Peter 2019-03-07 2019-03-11
Peter 2019-03-08 2019-03-19
Peter 2019-03-12 2019-05-22
Peter 2019-03-12 2019-10-31
Peter 2019-03-14 2019-04-05
Here is the data manipulation to show the net movements for one person; Peter.
x = pd.Series(df.groupby('Deposits').size())
y = pd.Series(df.groupby('Withdrawals').size())
balance = pd.DataFrame({'net_mov': x.sub(y, fill_value=0)})
balance = balance.assign(Peter=balance.net_mov.cumsum())
print(balance)
net_mov Peter
2019-03-07 1 1
2019-03-08 1 2
2019-03-11 -1 1
2019-03-12 2 3
2019-03-14 1 4
This works perfectly fine, and this is the format that I want to have. Now let's say I want to extend on this and not just list Peters deposits and withdrawals, but I want to add n-number of people. Lets assume that my dataframe looks like this:
df2.head()
name Deposits Withdrawals
Peter 2019-03-07 2019-03-11
Anna 2019-03-08 2019-03-19
Anna 2019-03-12 2019-05-22
Peter 2019-03-12 2019-10-31
Simon 2019-03-14 2019-04-05
The format I'm aiming for is this. I don't know how to group everything, and I don't know which names or how many columns there will be beforehand, so I can't hardcode names or number of columns. It has to be generate dynamically.
net_mov1 Peter net_mov2 Anna net_mov3 Simon
2019-03-07 1 1 1 1 2 2
2019-03-08 1 2 2 3 -1 1
2019-03-11 -1 1 0 3 2 3
2019-03-12 2 3 -2 1 4 7
2019-03-14 1 4 3 4 -1 6
UPDATE:
First off, thanks for the help. I'm getting closer to my goal. This is the progress:
x = pd.Series(df.groupby(['Created', 'name']).size())
y = pd.Series(df.groupby(['Finished', 'name']).size())
balance = pd.DataFrame({'net_mov': x.sub(y, fill_value=0)})
balance = balance.assign(balance=balance.groupby('name').net_mov.cumsum())
balance_byname = balance.groupby('name')
balance_byname.get_group("Peter")
Output:
net_mov balance
name Created Finished
Peter 2017-07-03 2017-07-06 1 1
2017-07-10 1 2
2017-07-13 0 2
2017-07-14 1 3
... ... ...
2020-07-29 2020-07-15 0 4581
2020-07-17 0 4581
2020-07-20 0 4581
2020-07-21 -1 4580
[399750 rows x 2 columns]
This is of course too many rows, the dataset I'm working with has around 2500 rows.
I've tried to unstack it but that creates problems on it's own.