hula-hula
hula-hula

Reputation: 119

Assign new string in the beginning of a text in a specific row and column pandas

I've been trying this for a while now, is there a possible way to append a new word in the beginning of the text in a specific row and column. For an example;

Body
 .
 .
 .
John, where are you?

Desired output:

Body
.
.
.
Hello John, where are you?

I've tried with:

rowIndex = df.index[109]
df.loc[rowIndex , 'Body'].append.str[:0].('Hello')

It didn't work however, appreciate your suggestion on this and thank you in advance!

Upvotes: 1

Views: 383

Answers (1)

jezrael
jezrael

Reputation: 862791

Use + for prepend new string:

df.loc[rowIndex , 'Body'] = 'Hello ' +  df.loc[rowIndex , 'Body']

Sample:

df = pd.DataFrame({'Body':['','','John, where are you?']}, index=[100,200,300])
print (df)
                     Body
100                      
200                      
300  John, where are you?

rowIndex = df.index[2]
df.loc[rowIndex , 'Body'] = 'Hello ' +  df.loc[rowIndex , 'Body']
print (df)
                           Body
100                            
200                            
300  Hello John, where are you?

Upvotes: 1

Related Questions