shearichard
shearichard

Reputation: 8372

Pandas dataframe col - efficient way to change "__" to "_" and "_" to " "?

I've got a column in a dataframe which need replacements made to the contents of the column as follows :

So for instance This_is_a_test_and_this_is_what_a_underscore_looks_like__! should result in This is a test and this is what a underscore looks like_!

What I've done so far is the easy bit ...

dk['name'].replace({'_': ' '}, regex=True, inplace=True)

... the only way I can think of to handle both parts of the requirement is to change the double underbars to some other character, say "^", before changing all the single underbars to spaces and then going back to change the "^" to underbars. This doesn't seem like it would be very efficient and presents the problem of the 'magic character' being present initially.

Anyone know of a better way to handle this ?

Upvotes: 1

Views: 322

Answers (2)

ThePyGuy
ThePyGuy

Reputation: 18436

You can pass a dictionary of values to the replace method of Series, with regex as True.

df['text'].replace({'__':'_', '_':' '}, regex=True)

0    This is a test and this is what a underscore l...
Name: text, dtype: object

Upvotes: 0

Shubham Sharma
Shubham Sharma

Reputation: 71687

We can try str.replace with a replacement lambda function

d = {'__': '_', '_': ' '}
df['name'] = df['name'].str.replace(r'(__|_)', lambda g: d[g.group()])

                                                        name
0  This is a test and this is what a underscore looks like_!

Upvotes: 4

Related Questions