Selecting highest rows on matrix pandas python.
<p>I have the following data:</p>

<p><a href="https://github.com/antonio1695/Python/blob/master/nearBPO/facturasb.csv" rel="nofollow">https://github.com/antonio1695/Python/blob/master/nearBPO/facturasb.csv</a></p>

<p>It is a matrix like the following example:</p>

<pre><code>UUID  A   B   C   D   E   F   G   H   I  
1.1   0   1   0   0   0   1   0   0   0
1.2   1   1   0   0   0   0   0   0   0
1.3   0   0   0   0   1   0   0   0   0
1.4   0   0   0   1   0   1   1   1   1
1.5   0   1   0   0   0   0   1   0   0
1.6   0   0   1   0   0   0   1   0   0 
1.7   0   1   0   0   0   0   0   1   0 
1.8   0   0   1   0   0   0   1   0   0
1.9   0   1   0   0   0   0   1   0   1
</code></pre>

<p>I would like to make a new matrix with only the 50 highest columns (3 in the example) and it's respective UUID. With the highest columns i mean those columns that have more 1's in the matrix. </p>

<p>If i'm not clear enough, don't hesitate asking. Thank you. </p>

<p>Let's break this task into two parts. First, find which columns have the most <code>1</code>'s. Second, select only those columns.</p>

<p>Here's some data:</p>

<pre><code>In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: import string

In [4]: data = np.random.randint(2, size=(10, 10))

In [5]: data
Out[5]:
array([[1, 1, 1, 1, 0, 1, 0, 0, 0, 0],
       [1, 1, 1, 1, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
       [0, 1, 0, 0, 1, 0, 0, 1, 1, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 1, 0],
       [0, 1, 0, 1, 1, 1, 0, 0, 1, 1],
       [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
       [1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
       [0, 0, 1, 1, 1, 0, 1, 1, 0, 1],
       [0, 0, 1, 1, 1, 0, 1, 0, 1, 1]]) 

In [6]: df = pd.DataFrame(data, columns=list(string.ascii_lowercase[:10]))

In [7]: df.index.name = 'uuid'

In [8]: df
Out[8]:
      a  b  c  d  e  f  g  h  i  j
uuid
0     1  1  1  1  0  1  0  0  0  0
1     1  1  1  1  0  1  0  0  0  0
2     0  0  0  1  1  1  0  0  0  0
3     0  1  0  0  1  0  0  1  1  0
4     0  0  0  1  0  0  0  0  1  0
5     0  1  0  1  1  1  0  0  1  1
6     0  0  1  1  1  1  1  1  0  0
7     1  1  1  1  1  0  1  1  1  1
8     0  0  1  1  1  0  1  1  0  1
9     0  0  1  1  1  0  1  0  1  1
</code></pre>

<p>Now let's find the columns with the most <code>1</code>'s.</p>

<pre><code>In [9]: df.sum()
Out[9]:
a    3
b    5
c    6
d    9
e    7
f    5
g    4
h    4
i    5
j    4
dtype: int64

In [10]: df.sum().sort_values(ascending=False)
Out[10]:
d    9
e    7
c    6
i    5
f    5
b    5
j    4
h    4
g    4
a    3
dtype: int64
</code></pre>

<p>Get the names of the first 3 of those.</p>

<pre><code>In [11]: df.sum().sort_values(ascending=False).index[:3]
Out[11]: Index(['d', 'e', 'c'], dtype='object')
</code></pre>

<p>Use those names to select columns from the original dataframe.</p>

<pre><code>In [12]: selection = df.sum().sort_values(ascending=False).index[:3]

In [13]: df[selection]
Out[13]:
      d  e  c
uuid
0     1  0  1
1     1  0  1
2     1  1  0
3     0  1  0
4     1  0  0
5     1  1  0
6     1  1  1
7     1  1  1
8     1  1  1
9     1  1  1
</code></pre>

python pandas matrix