Here's my dataset
Id Text
1 Animation_and_Cartoon - Comics and Anime/Cartoon_and_anime
2 Animation_and_Cartoon - Comics and Anime/Manga_and_anime
Expected output is all _
before -
is replaced by ' ', but after -
is not
Id Text
1 Animation_and_Cartoon - Comics and Anime/Cartoon_and_anime
2 Animation_and_Cartoon - Comics and Anime/Manga_and_anime
You can use:
df['Text'] = df['Text'].str.replace(
r'^([^-]+)',
lambda m: m.group().replace('_and_',' and '),
regex=True)
Output:
Id Text
0 1 Animation and Cartoon - Comics and Anime/Cartoon_and_anime
1 2 Animation and Cartoon - Comics and Anime/Manga_and_anime
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.