2017-03-08 55 views
-1

我试图将样本列与两个参考列D和R进行比较。如果样本匹配D或R,则用D或R替换该数据;除非./。在样本列中,我希望呼叫是NR。我已经添加了LogicCALL列在我的实际数据的数据帧到demonstrate--这些电话将取代(1,0,./。)numpy condition loop np.其中

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'