Group by column sum value with Pandas

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!

+3
source share
2 answers

Here you go:

In [57]: df.groupby(df.sum(), axis=1).sum()
Out[57]: 
   1  9
0  2  2
1  1  3
2  0  4

[3 rows x 2 columns]

df.sum() - . 0 (), : 1 ( a, b, c) 9 ( d). (axis=1) .

+8

pandas , , , . - -. , -:

dat = {'a':[1,0,0], 'b':[0,1,0], 'c':[1,0,0], 'd':[2,3,4]}
df = pd.DataFrame(dat)

df = df.transpose()
df['totals'] = df.sum(1)

print df.groupby('totals').sum().transpose()
#totals  1  9
#0       2  2
#1       1  3
#2       0  4
0

All Articles