1

I have a DataFrame that looks like this:

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

In actuality there are around 40K rows. What I want to do is to change the all the values in Foldchange with same value 2.

So that it will looks like:

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

How can I do that conveniently in Pandas?

Stefan
  • 41,759
  • 13
  • 76
  • 81
neversaint
  • 60,904
  • 137
  • 310
  • 477

3 Answers3

5

You should be able to simply:

df.loc[:, 'Foldchange'] = 2
Stefan
  • 41,759
  • 13
  • 76
  • 81
4

Similar to Stefan, you can also do

df['Foldchange']=2.0
NinjaGaiden
  • 3,046
  • 6
  • 28
  • 49
-2

I don't know pandas, but a quick search: Python pandas equivalent for replace

Since read_table returns a dataframe, you should be able to do a list comprehension or map() with the result() method.

Community
  • 1
  • 1
  • Since the question is, "How can I do that conveniently in Pandas?" and your response starts with, "I don't know pandas" - How about electing not to post. At this point, removing this response might be helpful since it doesn't add value to the conversation. – Software Prophets Jul 15 '17 at 19:13
  • The link provided is to another SO which answered the question as initially asked with several examples and two strong answers. I do not need Pandas experience to recognize a duplicate/similar question which could answer the posters question and avoid people re-explaining something. If anything, the constructive criticism you point out is that admitting no experience with the product does not inform the usefulness of the link and should have been omitted. – A Small Shell Script Jul 17 '17 at 21:24