2016-03-29 3 views
0

DataFrame.iterrows()を使用してDataFrameの行を繰り返し処理しています。行が特定の基準を満たしていれば、他のDataFrameに格納します。 set.difference(another_set)のように両方に表示される行を削除する方法はありますか?pd.DataFrameの一部をPythonで削除する

私は質問に答えがわからないので、私は問題を解決し、2つのDataFramesを持つ代わりに良いデータを保存して、別のDataFrameを作成しました。それら両方。

def test_right_chain(self, temp): 
    temp__=pd.DataFrame() 
    temp_=pd.DataFrame() 
    key=temp["nr right"].iloc[0] 
    temp_=temp_.append(temp.iloc[0]) 
    temp=temp[1:] 
    for index, row in temp.iterrows(): 
     print row 
     key_=row['nr right'] 
     if abs(key_-key)==1: 
      pass 
     elif len(temp_)>2: 
      print row 
      temp__.append(temp_) 
      temp_=pd.DataFrame() 
     else: 
      temp_=pd.DataFrame() 
     temp_=temp_.append(row) 
     key=key_ 
    return temp__ 
+1

問題を再現して助けてくれるように、入力コードの少なくとも一部と予想される出力を投稿する必要があります。 –

+0

__text__形式で5-7行のサンプル入力データセットと期待される出力を提供すると – MaxU

答えて

0

あなたは左のデータフレーム内の行で表示されたインデックスを残し、df.merge(df1, df2, right_index=True, how='inner')機能を両方データフレームの交差点を行う(私はなぜ知らないが、私はright_index=Trueを使用するときにこの問題が発生した)、その後のインデックスを取得することができますそれらの行今、あなたは、両方のデータフレームに表示された行のインデックスを必要とする

df1 = pd.DataFrame(np.random.rand(10,4),columns=list('ABCD')) 

df2 = df1.ix[4:8] 
df2.reset_index(drop=True,inplace=True) 
df2.loc[-1] = [2, 3, 4, 5] 
df2.loc[-2] = [14, 15, 16, 17] 
df2.reset_index(drop=True,inplace=True) 

df3=pd.merge(df1, df2, on=['A', 'B', 'C', 'D'], right_index=True, how='inner') 

:(Compare Python Pandas DataFrames for matching rows私はこの質問から答えを使用):

indexes= df3.index.values 

をそして、あなたは自分のデータフレームからそれらの行をドロップする必要があります。

df1=df1.drop(df1.index[indexes]) 
関連する問題