Python 交换2个数据帧列中的单元格字符串
我正在努力交换数据帧中两列的值,如下所示:Python 交换2个数据帧列中的单元格字符串,python,dataframe,pandas,compare-and-swap,Python,Dataframe,Pandas,Compare And Swap,我正在努力交换数据帧中两列的值,如下所示: 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
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)
一种方法是使用: 如果只想更改一列中的值,仍可以使用
apply
:
def my_fun2(row, colID):
if row[colID][0] == 'rs640249':
return row[colID][::-1] #reverse the tuple
else:
return row[colID]
df[colID] = df.apply(lambda x: my_fun2(x, colID), axis=1)
注意:由于my_fun2
返回单个值,因此这次apply
返回一个序列,因此我们需要稍微更改应用的方式
例如:
df
# 0
# 0 ('rs649071', 'rs640249')
# 1 ('rs640249', 'rs11073074')
df[0] = df.apply(lambda x: my_fun2(x,0), axis=1)
# 0
# 0 ('rs649071', 'rs640249')
# 1 ('rs11073074', 'rs640249')
一种方法是使用: 如果只想更改一列中的值,仍可以使用
apply
:
def my_fun2(row, colID):
if row[colID][0] == 'rs640249':
return row[colID][::-1] #reverse the tuple
else:
return row[colID]
df[colID] = df.apply(lambda x: my_fun2(x, colID), axis=1)
注意:由于my_fun2
返回单个值,因此这次apply
返回一个序列,因此我们需要稍微更改应用的方式
例如:
df
# 0
# 0 ('rs649071', 'rs640249')
# 1 ('rs640249', 'rs11073074')
df[0] = df.apply(lambda x: my_fun2(x,0), axis=1)
# 0
# 0 ('rs649071', 'rs640249')
# 1 ('rs11073074', 'rs640249')
对于未来的参考,这里有一个可能的解决方案:
for row_index, row in df.iterrows():
if row['L1'] == 'rs640249':
df.set_value(row_index, 'L1' , row['L2'])
df.set_value(row_index, 'L2' , row['L1'])
最好的,对于将来的参考,这里有一个可能的解决方案:
for row_index, row in df.iterrows():
if row['L1'] == 'rs640249':
df.set_value(row_index, 'L1' , row['L2'])
df.set_value(row_index, 'L2' , row['L1'])
最好,为什么不试试这样的方法,使用数组操作:
condition = df['L1'] == 'rs640249'
tmp = df['L1'].copy()
df['L1'][condition] = df['L2'][condition]
df['L2'][condition] = tmp[condition]
为什么不试试这样的阵列操作:
condition = df['L1'] == 'rs640249'
tmp = df['L1'].copy()
df['L1'][condition] = df['L2'][condition]
df['L2'][condition] = tmp[condition]
您可以使用
list(tup)
将元组转换为列表并进行切换。您可以使用list(tup)
将元组转换为列表并进行切换。嗨,海登,谢谢您的帮助。那正是你喜欢做的。然而,它似乎对我不起作用。我之所以使用索引,是因为行是元组(如果行[0]='rs…):行[0],行[1]=行[1],行[0])。此外,元组是可调的!这就是事情不顺利的原因。这个问题还没有解决。谢谢你的帮助。啊哈!它们是元组:)已更新,这应该可以修复,而且我在以前的代码中缺少了一个返回值
!嗨,海登,谢谢你的帮助。那正是你喜欢做的。然而,它似乎对我不起作用。我之所以使用索引,是因为行是元组(如果行[0]='rs…):行[0],行[1]=行[1],行[0])。此外,元组是可调的!这就是事情不顺利的原因。这个问题还没有解决。谢谢你的帮助。啊哈!它们是元组:)已更新,这应该可以修复,而且我在以前的代码中缺少了一个返回值
@海登:谢谢你的评论,也许这不是最好的方式,但是,它工作得很好。当然,iterrows将创建不必要的系列,然而,我无法按照您的建议使事情正常工作:我有空数据帧和一堆错误。我怀疑pandas版本(0.8.1)和/或python版本(3.2)。你测试了吗?真奇怪。您正在使用哪些pandas和python版本?@hayden:如果您感兴趣,请查看@hayden:谢谢您的评论,也许这不是最好的方式,但是,它工作得很好。当然,iterrows将创建不必要的系列,然而,我无法按照您的建议使事情正常工作:我有空数据帧和一堆错误。我怀疑pandas版本(0.8.1)和/或python版本(3.2)。你测试了吗?真奇怪。您使用的熊猫和python版本是什么?@hayden:如果您感兴趣,请查看