2017-04-10 4 views
0

그래서 데이터 프레임을 그룹화 한 다음 함수를 적용하고 있습니다. 이제 프레임의 각 행을 확인하여 데이터 프레임의 나머지 행에 대해 확인하고 일부 조건과 일치하는 경우 태그의 일종으로 다른 데이터 프레임에 추가하고 원본에서 제거하고 싶습니다. 그것은 조건을 통과하지 않으면 내가 거기에 행을 유지하고 다음 행으로 이동합니다.데이터 프레임의 행을 반복하고 나머지 행과 비교하십시오.

예를 들어

 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()를 사용하는 것과에서 함수를 작성하는 경우 파이썬. , 구문에 대한

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

(100 % 확인 :

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

당신은 또한 당신의 상태를 평가하는 부울 값 플래그 열을 만들 수 있습니다, 다음과 같은 true로 설정하는 값을 가진 모든 행을 나타 수중에 통역관이 없다)

관련 문제