Skip to main content
 首页 » 编程设计

python之如何在 Pandas 中为具有相同值的每一行替换列中的值

2025年05月04日33Leo_wl

我有一个看起来像这样的 DataFrame:

In [10]: import pandas as pd 
In [11]: df = pd.read_table("http://dpaste.com/2M75260.txt",sep=",") 
In [12]: df.head(n=3L) 
Out[12]: 
             ID    Genes  Foldchange 
0    1415670_at     Copg       0.989 
1    1415673_at     Psph       1.004 
2  1415674_a_at  Trappc4       1.000 

实际上大约有 40K 行。我想要做的是将 Foldchange 中的所有值更改为相同的值 2。

所以它看起来像:

ID            Genes  Foldchange 
1415670_at     Copg       2.000 
1415673_at     Psph       2.000 
1415674_a_at  Trappc4     2.000 
.... etc... 

我怎样才能在 Pandas 中方便地做到这一点?

请您参考如下方法:

你应该能够简单地:

df.loc[:, 'Foldchange'] = 2