I have created a new aggregated dataframe by using groupby and I am having problems with adding a subtotal row under each category.
I have tried using pd.groupby and pivottable and changing the index but I don't manage to represent the data as I want it.
- Creates a subtotal of "USD_Balance" for each "Client", but add it as a column:
df_balance['Subtotal'] = df_balance.groupby('Client')['USD_Balance'].transform('sum')
- Creating a groupby and merging with my raw table gives me the same result.
+----------+-------------+------------+
|CLient ID | USD_Balance | Subtotal |
+----------+---------+------------+----
| 1 | 2 | 6 |
| 1 | 2 | 6 |
| 1 | 2 | 6 |
+----------+-------------+------------+
- How I would like to display my data:
|---------------------|------------------|
| Client ID | USD_Balance |
|---------------------|------------------|
| 1 | 2 |
|---------------------|------------------|
| 1 | 2 |
|---------------------|------------------|
| 1 | 2 |
|---------------------|------------------|
| SubTotal | 6 |
|---------------------|------------------|
I would like to add a Subtotal row, with the corresponding agg per Client ID group.
Thanks in advance for any pointers on how to present my data like this!