python - 有条件地交换 pandas 中的值

标签 python pandas

我有一个 df 例如:

qseqid  sseqid  pident  length  mismatch    gapopen qstart  qend    sstart  send    evalue  bitscore
scaffold_1  test    25.5    337 166 9   2   20  27  310 1.0e-16 94.0
scaffold_1  tes2    27.0    278 163 9   500 20  222 487 1.e-16  94.0
scaffold_1  tes4    35.5    166 93  2   3   10  63  227 2.e-15  89.7

我想更改 df.qstart 中的值和ds.qend如果df.qstart>df.qstart

这里是:

qseqid  sseqid  pident  length  mismatch    gapopen qstart  qend    sstart  send    evalue  bitscore
    scaffold_1  test    25.5    337 166 9   2   20  27  310 1.0e-16 94.0
    scaffold_1  tes2    27.0    278 163 9   20 500  222 487 1.e-16  94.0
    scaffold_1  tes4    35.5    166 93  2   3   10  63  227 2.e-15  89.7 

如您所见,500 被放置在 20 的位置 感谢您的帮助

最佳答案

您可以对值进行排序并将它们分配回来:

import numpy as np
df[['qstart', 'qend']] = np.sort(df[['qstart', 'qend']].values, axis=1)

关于python - 有条件地交换 pandas 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54312193/

相关文章:

python - 如何让 Scrapy 只抓取一页(使其非递归)?

python - 为什么 pdb 不能访问包含异常的变量?

python - Python 方法内定义的数据帧和变量的存储

python - 扁平化 python 数据框中的条目,例如 Apache PIG 包

python - 如何替换 pandas 数据框中看起来相似的值?

javascript - 如何在Django模板中刷新表格

python - 为什么 error_messages 在元类中不起作用?

python - 如何使用 Boto3 查找没有标签的实例

python - Pandas 系列中的尾随字符 - 如何使符号后缀成为前缀?

python - 使用 pandas 和 scipy 的树状图