# How can I group by elements in a column in pandas? [closed]

I have a dataset that looks like this:

I need to transform it so it looks like this:

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:

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

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.