2017-04-10 100 views
0

所以我有一個數據框,我將它分組,然後對其應用一個函數。現在我想檢查框架中的每一行,檢查數據框中的其餘行是否符合某些條件,我希望將它們添加到具有某種標籤的不同數據框中,並將其從原始列表中刪除。如果它不通過條件,我保留那裏的行並移動到下一行。迭代Dataframe中的行並將其與其餘行進行比較

 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 

,所以我這樣做

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

如果您需要的功能(F)有副作用,我會使用df.iterrows()和寫入功能蟒蛇。

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

你也可以創建一個布爾值,評估你的病情標誌列,然後在彈出具有該值設置爲true的行:

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

(不是100%肯定有關語法,目前沒有口譯員)

相關問題