ah bon
ah bon

Reputation: 10051

How to strip punctuation of at the beginning and the end of string column in Pandas

I have a dataframe in which some rows have unecessary punctuations like , ? . etc in the beginning and the end of it string type columns, how can strip those punctuation use Pandas? Thanks.

id   price    fruits
01     1       .apple
02     2       ,apple, banana?
03     3       ?orange?

It should be like this

id   price    fruits
01     1       apple
02     2       apple, banana
03     3       orange

Upvotes: 2

Views: 373

Answers (1)

jezrael
jezrael

Reputation: 863301

Use str.strip with punctuation:

import string
df['fruits'] = df['fruits'].str.strip(string.punctuation)
print (df)
   id  price         fruits
0   1      1          apple
1   2      2  apple, banana
2   3      3         orange

print (string.punctuation)
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

EDIT:

For custom punctuation is possible use string, only necessary escape " if value for check:

df['fruits'] = df['fruits'].str.strip(",\"?'.")
print (df)

   id  price         fruits
0   1      1          apple
1   2      2  apple, banana
2   3      3         orange

Upvotes: 3

Related Questions