2017-03-08 1 views
-1

샘플 열을 두 개의 참조 열 D와 R과 비교하려고합니다. 샘플이 D 또는 R과 일치하면 해당 데이터를 D 또는 R로 바꿉니다. 않는 한 ./. 샘플 열에 다음 호출을 NR 원하는. 나는 (./, 1,0.) 그 통화를 대체 할 내 실제 데이터 dataframe에 demonstrate--하는 LogicCALL 열을 추가 한 나는 아래의 루프를 구성이이 점에numpy 조건 루프 np.where

ReferenceD ReferenceR sample LogicCALL 
0   1   0  1   D 
1   1   1 ./.  NC 
2   1   0  0   R 
Index(['ReferenceD', 'ReferenceR', 'sample', 'LogicCALL'], dtype='object') 

; 여기서 Alt는 샘플 목록입니다. 루프는 D와 R은 호출하지만 NC는 호출하지 않고 대신 스크립트는 "R"을 반환합니다.

for sample in Alt: 
     gtdata[(sample)] = np.where((gtdata[(sample)] == gtdata['ReferenceD']) & (gtdata[sample] != gtdata['ReferenceR']), "D", 
           np.where((gtdata[(sample)] == "D") & (gtdata[(sample)] is not ('\./.')), "D", 
              np.where((gtdata[(sample)] == "D") & (gtdata[(sample)].str.contains('\./.')), "NC", 
                "R"))) 
+1

내가 루프를 볼 수 없습니다. 중첩되거나 순차적 인'where'를 봅니다. 어느 것을 알기가 어렵습니다. 나는 그것을 편집자에게 복사해서 읽을 수있는 조각으로 나누어야 할 것이다. – hpaulj

답변

0

그것은 기능 구문하지만 절차 적 assignemnts을하는 것이 작업을 수행 할 수있는 가장 읽을 수있는 방법이 아니다 :

df.loc[df['ReferenceD'].astype(str) == df['sample'], 'LogicCALL'] = 'D' 
df.loc[df['ReferenceR'].astype(str) == df['sample'], 'LogicCALL'] = 'R' 
df.loc[df['sample'] == './.',      'LogicCALL'] = 'NR'