2017-04-10 6 views
0

データフレームがあります。これはグループ化してから関数を適用しています。今、私は、フレーム内の各行をチェックしたいのですが、データフレームの残りの行と照合し、いくつかの条件に合致すれば、それらを別のデータフレームに追加して元のタグから削除したいと思います。それは条件を渡さない場合は、私はそこに行を維持し、次の行に移動します。データフレームの行を繰り返し処理し、それを残りの行と比較します。

例えば

 time  status  number  action  fname lname 
0  10.30  Active  2   0   Adrian Peter 
1  11.01  Active  3   2   Peter Thomas 
2  11.05  Passive  2   0   Thomas Adrian 
3  11.07  Passive  2   1   Jen  Anniston 

ので、私はあなたの希望の関数(f)は副作用を持っている場合

df.groupby(status).apply(f) 

def f(x): 
    I want to perform some tasks here and with the remaining dataframe 
    i want to see if index 0 has similar number and action in the 
    remaining data frame. If true i want to put this in a different dataframe and tag it and remove the pair from the origial df. 
    I want to then move on to the next index and do the same. If false after looking at all the data in the frame i want to delete this from the original df too 
+2

入力例にいくつかのサンプル出力を追加すると、さらに簡単になるかもしれません。 – miradulo

答えて

1

ような何かを、私は(df.iterrowsを使用したい)とで関数を書きますPython。

for index, row in df.iterrows(): 
    # Do stuff 

また真として設定し、その値を持つすべての行ポップ、その後、あなたの状態を評価するブール値にフラグ列を作成することができます

df['tagged'] = df.apply(lambda row: <<condition goes here>>, axis=1) 
tagged_rows = df[df['tagged'] == True] 
df = df[df['tagged'] != True] 

構文について(100%確実ではないが、

関連する問題