1

I have a dataframe with over 5M rows. I am concerned with just one column in this dataframe. Let's assume dataframe name to be df and the column in consideration to be df['id']. An example of the dataframe is shown below:

df['id'] :

    id
0   432000000
1   432000010
2   432000020

The column df['id] is stored as a string.

I want to add a prefix to all the rows of a particular column in this dataframe. Below is the code I use:

 for i in tqdm(range(0,len(df['id']))):
       df['id'][i]='ABC-1234-'+df['id'][i]

While the above code works, it shows 15 hours to complete. Is there a more efficient way to perform this task ?

Gary
  • 909
  • 8
  • 20
  • Please have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide some sample data with the desired output. There are a number of ways to approach the problem, but especially with string operations, it may be highly dependent on the input data – G. Anderson Sep 11 '19 at 15:38
  • 4
    `df['id'] = 'ABC-1234-' + df['id']`? – Quang Hoang Sep 11 '19 at 15:40
  • Or if the text needs to be replaced somewhere inbetween, you can also use `df['id'].map('ABC-1234-{}'.format)`. But I guess @QuangHoang's version is faster because it doesn't need a `map` call. – jottbe Sep 11 '19 at 15:42
  • @QuangHoang's version works like a charm! – Gary Sep 11 '19 at 15:49

0 Answers0