簡體   English   中英

遍歷數據框中的行,並將其與其余行進行比較

[英]Iterate over rows in a Dataframe and compare it to the rest of the rows

所以我有一個數據框,我將其分組,然后對其應用功能。 現在,我想檢查框架中的每一行,以檢查數據框中的其余行,如果匹配某些條件,我想使用某種標簽將它們添加到其他數據框中,並將它們從原始標簽中刪除。 如果沒有通過條件,我將這些行保留在那里,然后繼續進行下一行。

例如

      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

如果您想要的函數(f)有副作用,我將使用df.iterrows()並在python中編寫該函數。

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%確定語法,手邊沒有解釋器)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM