简体   繁体   中英

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

So i have a dataframe, which i am grouping by and then applying a function to it. Now i want to check for each row in the frame check that against the remaining rows in the dataframe and if it matches some conditions i would like to add them to a different dataframe with some sort of tag and remove them from the orginal. If it doesnt pass the conditions i keep the rows there and move on to the next row.

eg

      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

so i do something like

 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

If your desired function (f) has side-effects, i'd use df.iterrows() and write the function in python.

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

You can also create a flag column with a boolean value evaluating your condition, then pop all rows that have that value set as true:

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

(not 100% sure about the syntax, don't have an interpreter on hand)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM