Giacomo F
Giacomo F

Reputation: 132

Delete abbreviations (combination of Letter+dot) from Pandas column

I'd like to delete specific parts of strings in a pandas column, such as any letter followed by a dot. For example, having a column with names:

John W. Man
Betty J. Rule
C.S. Stuart

What should remain is

John Man
Betty Rule
Stuart

SO, any letter followed by a dot, that represents an abbreviation, should go. I can't think of a way with str.replace or anything like that.

Upvotes: 1

Views: 72

Answers (1)

jezrael
jezrael

Reputation: 863301

Use Series.str.replace with reegx for match one letter with . and space after it if exist:

df['col'] = df['col'].str.replace('([a-zA-Z]{1}\.\s*)','', regex=True)
print (df)
          col
0    John Man
1  Betty Rule
2      Stuart

Upvotes: 1

Related Questions