1

私は使用しpandas groupbyていて、以下を実装する方法を考えていました:

  1. データフレームAとBにはインデックスを付けるための同じ変数がありますが、Aには20の一意のインデックス値があり、Bには5があります。

  2. インデックスがBではなくAに存在する行を含むデータフレームCを作成したいと思います。

  3. Bの5つの一意のインデックス値がすべてAに存在すると仮定します。この場合、Cには、BではなくAのインデックス値に関連付けられた行のみが含まれます(つまり、15)。

  4. 内側、外側、左、右を使用してこれを行わないでください(私が何かを読み間違えた場合を除きます)。

SQLではこれを次のように行う可能性がありますwhere A.index <> (not equal) B.index

私の左利きの解決策:

a)各データセット(xとyなど)からそれぞれのインデックス列を取得します。

def match(x、y、compareCol):

"""

x and y are series

compare col is the name to the series being returned .

It is the same name as the name of x and y in their respective dataframes"""

x = x.unique()

y = y.unique()

""" Need to compare arrays x.unique() returns arrays"""

new = []

for item in (x):

    if item not in y:

        new.append(item)

returnADataFrame = pa.DataFrame(pa.Series(new, name = compareCol))

return returnADataFrame

b)データセットAでこれに対して左結合を実行します。

私の要素ごとの比較は、やる気のない雑草のカメのように遅いと合理的に確信しています。

4

1 に答える 1

1

次のようなものはどうですか?

A.ix[A.index - B.index]

A.index - B.indexset違いです:

    In [30]: A.index
    Out[30]: Int64Index([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], dtype=int64)

    In [31]: B.index
    Out[31]: Int64Index([  0,   1,   2,   3, 999], dtype=int64)

    In [32]: A.index - B.index
    Out[32]: Int64Index([ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], dtype=int64)

    In [33]: B.index - A.index
    Out[33]: Int64Index([999], dtype=int64)
于 2012-05-28T07:51:50.383 に答える