迭代Dataframe中的行并将其与其余行进行比较

时间:2017-04-10 14:57:14

标签: python pandas numpy dataframe

所以我有一个数据帧,我正在分组,然后应用一个函数。现在我想检查帧中的每一行检查数据帧中的剩余行,如果它匹配某些条件,我想将它们添加到具有某种标记的不同数据帧并从原始中删除它们。如果它没有通过条件我将行保留在那里并继续前进到下一行。

e.g

      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

1 个答案:

答案 0 :(得分:1)

如果你想要的函数(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%确定语法,手头没有解释器)