python - 更改特定列中的值(pandas)

标签 python pandas

我只是想更改特定列中的值

gene     scaf    GC     other
gene1    3456    78      other1
gene2    7898    56      other 2
gene3    5667    23      other 3

等 我想在每个基因名称处添加以下数字:_number1 并得到:

gene             scaf    GC      other
gene1_number1    3456    78      other1
gene2            7898    56      other 2
gene3            5667    23      other 3

谢谢大家。

最佳答案

您可以按条件添加字符串:

df.loc[df['gene'] == 'gene1', 'gene'] += '_number1'

print (df)
            gene  scaf  GC    other
0  gene1_number1  3456  78   other1
1          gene2  7898  56  other 2
2          gene3  5667  23  other 3

关于python - 更改特定列中的值(pandas),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50304937/

相关文章:

仅当值不为 None 时才应用函数的 Python 习惯用法

python - 旋转 numpy 二维数组

python - 将 pandas 数据帧的一行转换为多行

python - 为什么 hash() 在 python3.4 和 python2.7 下比较慢

python - 是否有在 __init__ 方法中定义变量的最佳实践

python - 如何使用 python 和 boto3 读取不是我自己的存储桶?

python - 使用 df.apply 或 Pandas 中的类似方法修改行值的函数

python - Groupby 和 any() |全部()

python - pd.cut 可以一起使用间隔范围和标签吗?

python - 使用实际数据在图上添加回归线