我正在努力从数据框的 2 列交换值,如下所示:
rs649071 rs640249 0.265 0.49
rs647621 rs640249 0.227 0.34
rs644339 rs640249 0.116 0.08
rs641563 rs640249 1.0 33.96
rs640249 rs11073074 0.248 0.77
rs640249 rs11637397 0.194 0.68
这个想法是测试第 2 列的每个单元格是否为 rs640249,如果不是,则更改为第 1 列中的相应字符串,反之亦然。这样,最终结果将类似于:
rs649071 rs640249 0.265 0.49
rs647621 rs640249 0.227 0.34
rs644339 rs640249 0.116 0.08
rs641563 rs640249 1.0 33.96
rs11073074 rs640249 0.248 0.77
rs11637397 rs640249 0.194 0.68
我试图迭代元组,但是,元组不支持项目分配。
rscode='rs640249'
for inf in LDfiles:
df = read_csv(inf, sep='\t', skiprows=1, names=['A', 'B', 'C'])
for tup in df.itertuples():
if tup[2] != rscode:
tup[1], tup[2] = tup[2], tup[1]
print(tup)