简体   繁体   中英

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!

你可以做

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM