Dance Party
Dance Party

Reputation: 3723

Pandas DF: Replace Middle Part of String

Given the following DataFrame:

import pandas as pd
DF = pd.DataFrame({'LastFirst':['Last , First','Last , First','Last , First',
'Last , First','Last , First','Last , First','Last , First']})
DF
    LastFirst
0   Last , First
1   Last , First
2   Last , First
3   Last , First
4   Last , First
5   Last , First
6   Last , First

I would like to know the most efficient (fastest computing) way to replace the " , " between Last and First with ", " (get rid of just the space before the comma but keep the one after it).

Thanks in advance!

Upvotes: 0

Views: 193

Answers (1)

chrisaycock
chrisaycock

Reputation: 37928

You could just do

DF.LastFirst = DF.LastFirst.str.replace(' ,', ',')

Upvotes: 3

Related Questions