1

I want to add a particular string in each element of a column in a Dataframe. For example, if I add string 'XXXX' then

StringColumn    | ResultingCOlumn
346fdf464f6ad4f | XXXX346fdf464f6ad4f
135af34343dsa4d | XXXX135af34343dsa4d
31d344fagtru64u | XXXX31d344fagtru64u
yaj6j4y646jo4we | XXXXyaj6j4y646jo4we

Can somebody help in telling me what particular method will be applied in Pandas dataframe to do this ? Thanks.

muazfaiz
  • 4,611
  • 14
  • 50
  • 88
  • 1
    Does this answer your question? [add a string prefix to each value in a string column using Pandas](https://stackoverflow.com/questions/20025882/add-a-string-prefix-to-each-value-in-a-string-column-using-pandas) – My Work Dec 21 '20 at 06:17

2 Answers2

1

Use the .apply method to create a new column with the appended string.

df['ResultingColumn'] = df.StringColumn.apply(lambda s: 'XXX'+s)
James
  • 32,991
  • 4
  • 47
  • 70
0

I did it like this and it works

df['ResultingColumn'] = ['XXX'+str(x) for x in df['StringColumn']]
muazfaiz
  • 4,611
  • 14
  • 50
  • 88