1

誰かが問題の解決策を考え出すのを手伝ってくれるかどうか疑問に思っています。私は基本的に、一連の pandas の文字列操作を使用して作成した pandas の一連のリスト (スペース区切り) を持っています - str.split(' ')。これらの各リストと別のリストの交差点である別の一連のリストを作成する必要があります。

ここで apply() が欠落していると思いますが、エラーが発生しているため、私の使用法は間違っているに違いありません。apply() でセット操作を使用することは、パンダのマニュアルでは実際にはカバーされていませんが、実行できるはずだと思いますか?

基本的に、一連のイベント (evector) があり、特定のイベント (e2) でユーザーを共有するイベントのベクトルを設定したいと考えています。これらはこれまでの私のアプローチでした:

元の試み:

evector = attendframe.yes.str.split(' ') #creates the series of lists

e2 = [attendframe.yes[attendframe.event==686467261]] #just for testing - returns [0
  #  1975964455 252302513 4226086795 3805886383 142...
  #Name: yes]

sharedvector = evector.apply(lambda x: [n for n in [x] if n in e2]) # the important bit

print sharedvector

エラー: 配列の長さが異なります: 1 対 7

問題を次の行に絞り込みました。 evector = attendframe.yes.str.split(' ').apply(lambda x: set([x]))

それから私はそれを正しくするためにさらにいくつかの試みをしました。

試行 1

evector = attendframe.yes.str.split(' ').apply(lambda x: set([x]))
#Unhashable type "list"

試行 2

evector = attendframe.yes.str.split(' ').apply(lambda x: set(x))
#TypeError: 'float' object is not iterable

試行 3 (クレジットは Andy Hayden)

evector = attendframe.yes.str.split(' ').apply(lambda x: x
                                                if isinstance(x, float)
                                                else set(x))

e2 = set([2394228942, 2686116898, 1056558062, 379294223])
sharedvector = evector.apply(lambda x: x if isinstance(x, float) else x.intersection(e2))
sharedvector.dropna())
#works, but returns empty arrays.

そして、これはデータ自体が問題を引き起こしているサンプルです:

print attendframe.yes.str.split(' ')

0     [1975964455, 252302513, 4226086795, 3805886383...
1     [2394228942, 2686116898, 1056558062, 379294223...
2                                                   NaN
3                                                   NaN

最終的なソリューションに関連する場合は、最終的に、マージンにイベントが含まれ、セルに2つの特定のイベント間で共有されるユーザーのリストが含まれるデータフレームを作成したいと思います。列ベクトルの生成はその最初の部分です。次に、関数全体で同様の apply() ステップを実行して、完全な行列を作成したいと考えています。

4

1 に答える 1

1

集合演算について質問しているので、setオブジェクトを使用しない理由は次のとおりです。

evector = attendframe.yes.str.split(' ').apply(set)
e2 = set(attendframe[attendframe.event==686467261]]['yes'])

設定された交差点を適用します:

sharedvector = evector.apply(lambda x: x & e2)

データに含まれているNaN場合は、各 set 呼び出しをラップして、それが float であるかどうかをテストできます。

evector = df.yes.str.split(' ').apply(lambda x: x
                                                if isinstance(x, float)
                                                else set(x))
e2 = set(attendframe[attendframe.event==686467261]]['yes'])
sharedvector = evector.apply(lambda x: x if isinstance(x, float) else x & e2)
于 2013-02-25T13:49:53.923 に答える