2

I have a pandas data frame with thousands of rows and 4 columns. i.e.:

A B C D 
1 1 2 0
3 3 2 1
3 1 1 0
....

Is there any way to count how many times a certain row occurs? For example how many times can [3,1,1,0] be found, and return the indices of those rows?

4

3 に答える 3

4

あなたが1つの行だけを探しているなら、私は次のようなことをするかもしれません

>>> df.index[(df == [3, 1, 1, 0]).all(axis=1)]
Int64Index([2, 3], dtype=int64)

-

説明は次のとおりです。から始まる:

>>> df
   A  B  C  D
0  1  1  2  0
1  3  3  2  1
2  3  1  1  0
3  3  1  1  0
4  3  3  2  1
5  1  2  3  4

目標と比較します。

>>> df == [3,1,1,0]
       A      B      C      D
0  False   True  False   True
1   True  False  False  False
2   True   True   True   True
3   True   True   True   True
4   True  False  False  False
5  False  False  False  False

一致するものを見つけます:

>>> (df == [3,1,1,0]).all(axis=1)
0    False
1    False
2     True
3     True
4    False
5    False

そして、このブールシリーズを使用して、インデックスから選択します。

>>> df.index[(df == [3,1,1,0]).all(axis=1)]
Int64Index([2, 3], dtype=int64)

1つの行の発生をカウントせず、代わりに各行に対してこれを繰り返し実行したいので、本当にすべての行を同時に検索したい場合は、上記を何度も繰り返すよりもはるかに高速な方法があります。しかし、これは1行で十分に機能するはずです。

于 2013-03-16T19:57:41.813 に答える
1

First create a sample array:

>>> import numpy as np
>>> x = [[1, 1, 2, 0],
... [3, 3, 2, 1],
... [3, 1, 1, 0],
... [0, 1, 2, 3],
... [3, 1, 1, 0]]

Then create a view of the array where each row is a single element:

>>> y = x.view([('', x.dtype)] * x.shape[1])
>>> y
array([[(1, 1, 2, 0)],
       [(3, 3, 2, 1)],
       [(3, 1, 1, 0)],
       [(0, 1, 2, 3)],
       [(3, 1, 1, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<i8'), ('f3', '<i8')])

Do the same thing with the element you want to find:

>>> e = np.array([[3, 1, 1, 0]])
>>> tofind = e.view([('', e.dtype)] * e.shape[1])

And now you can look for the element:

>>> y == tofind[0]
array([[False],
       [False],
       [ True],
       [False],
       [ True]], dtype=bool)
于 2013-03-16T19:49:18.750 に答える
1

You can also use MultiIndex, when it's sorted, it is faster to find the count:

s = StringIO("""A  B  C  D
1  1  2  0
3  3  2  1
3  1  1  0
3  1  1  0
3  3  2  1
1  2  3  4""")
df = pd.read_table(s,delim_whitespace=True)
s = pd.Series(range(len(df)), index=pd.MultiIndex.from_arrays(df.values.T))
s = s.sort_index()
idx = s[3,1,1,0]
print idx.count(), idx.values

output:

2 [2 3]
于 2013-03-16T23:24:26.177 に答える