2

I have a sample data set:

 ID       sequence
H100      ATTCCT
H231      CTGGGA
H2002     CCCCCCA

I simply want to add a ">" in front of each ID:

 ID       sequence
>H100      ATTCCT
>H231      CTGGGA
>H2002     CCCCCCA

From this post Append string to the start of each value in a said column of a pandas dataframe (elegantly) I got the code :

df["ID"] = '>' + df["ID"].astype(str)

However, this warning message came up:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

so I tried:

 df.loc[: , "ID"] = '>' 

The same error message came up

How should i correct it this?

thanks

Community
  • 1
  • 1
Jessica
  • 2,923
  • 8
  • 25
  • 46

1 Answers1

8

Give this a shot - works for me in Python 3.5:

df['ID'] = ('>' + df['ID'])

If that won't do it, you may have to refer to df.iloc[:,1] for example (just type it in the terminal first to ensure you grabbed the right field where ID is located).

The other problem you may be experiencing is that your dataframe was created as a slice of another dataframe. Try converting your "slice" to its own dataframe:

dataframename = pandas.DataFrame(dataframename)

Then do the code snip I posted.

Best - Matt

Not sure why I'm losing reputation points for trying to answer questions for people with actually verified answers... kind of wondering what the point of this forum is at the moment.

Matt
  • 2,602
  • 13
  • 36
  • Try using my updated answer and let me know if it is clear enough. And if you actually have the code I can play with I can answer your question a lot faster (provided the above doesn't work). Please vote for my answer if it helps you out BTW, so far this post has only hurt my reputation for some strange reason. – Matt Apr 06 '16 at 17:55