I got lost in the Pandas doc and functions trying to figure out method groupbya DataFramefrom the sum of the columns.
let's say I have the following data:
In [2]: dat = {'a':[1,0,0], 'b':[0,1,0], 'c':[1,0,0], 'd':[2,3,4]}
In [3]: df = pd.DataFrame(dat)
In [4]: df
Out[4]:
a b c d
0 1 0 1 2
1 0 1 0 3
2 0 0 0 4
I would like to columns a, band cwere grouped together as they all have a sum equal to 1. The resulting DataFrame will have column labels, equal to the sum of columns, which he summed up. Like this:
1 9
0 2 2
1 1 3
2 0 4
Any idea to put me in the right direction? Thanks in advance!
source
share