0
$\begingroup$

I have a dataset that looks like this:

enter image description here

I need to transform it so it looks like this:

enter image description here

Meaning I need to show the balance by balance groups and time gaps.

Now the time gaps are elements of a column. I tried to use the following;

b = a.pivot_table(values='GAP', index=a.index, columns='BALANCE_GROUP', aggfunc='first')

With different parameters but it outputs something really weird. I also tried to use the groupby function which works fine, but it won't allow me to set the elements in the gap column as seperate columns...

b = a.groupby(['BALANCE_GROUP','GAP']).sum()

and I got something like this:

enter image description here

$\endgroup$
1
$\begingroup$

If I understand what you need, I think it is this:

b = a.pivot_table(values='TOTAL_BALANCE_EUR', index=['NSFR_GROUP', 'BALANCE_GROUP'], columns='GAP', aggfunc='sum')
b

It's easier for others to help you if you make the data available to others. Just make a tiny dataframe with 10 rows for instance. Also, you can make the code a bit easier to read by enclosing it in three backticks: ``` or using the code sample button when you write your post.

$\endgroup$
1
  • $\begingroup$ AMAZING!!! THANK YOU! $\endgroup$ – PlatinumMaths Apr 18 at 22:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.