选择矩阵pandas python上的最高行。

时间:2016-09-02 17:53:40

标签: python pandas numpy matrix

我有以下数据:

https://github.com/antonio1695/Python/blob/master/nearBPO/facturasb.csv

这是一个类似于以下示例的矩阵:

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

我想制作一个只包含50个最高列(示例中为3个)的新矩阵及其相应的UUID。对于最高列,我指的是矩阵中具有更多1的列。

如果我不够清楚,请不要犹豫。谢谢。

2 个答案:

答案 0 :(得分:3)

IIUC

df[df.sum().nlargest(3).index]

enter image description here

排除n个最大

中全零的行
n = df.sum().nlargest(3).index
df1 = df.loc[:, n]
df1[df1.eq(1).any(1)]

enter image description here

设置

from StringIO import StringIO
import pandas as pd

text = """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"""

df = pd.read_csv(StringIO(text), index_col=0, delim_whitespace=True)

带有numpy的奖金解决方案

假设设置相同(这可能更快)

n = df.values.sum(0).argsort()[-3:][::-1]
m = (a[:, n] == 1).any(1)

df.iloc[m, n]

注意 这些列与我的其他解决方案不同。这是因为多列总和为相同的值。

enter image description here

时序

enter image description here

答案 1 :(得分:0)

让我们将这项任务分为两部分。首先,找出哪些列具有最多1的列。其次,只选择那些列。

以下是一些数据:

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

现在让我们找到1个最多的列。

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

获取前3个的名称。

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

使用这些名称从原始数据框中选择列。

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