How to effeciently remove character if available at the beginning and end of a string in Pandas?

Viewed 105

The idea is to remove full stop, commas, quotation if it is available at the beginning and last string in Pandas.

Given a df as below

data = {'Name': ['"Tom hola.', '"nick"', 'krish here .','oh my *']}

The expected output is

Tom hola
nick
krish here
oh my

I tried the following code, but it did not work as intended

import pandas as pd
df = pd.DataFrame(data)
df['Name'] = df['Name'].str[-1:].replace({"\. ": "Na"},regex=True)

May I know how this objective can be achieved?

Also, can the approach extended for it to be applied across different columns?

2 Answers

You can use pd.Series.str.replace if you want replace only colum else use df.replace.

# Using `pd.Series.str.replace`
df['Name'] = df['Name'].str.replace(r'\.$','')
df          Name
0     Tom hola
1   secondx //
2         nick
3  krish here

# Using `df.replace`
df.replace(r'\.$', '', regex=True)
          Name
0     Tom hola
1   secondx //
2         nick
3  krish here

EDIT:

You can use pd.Series.str.strip to strip ", . and *

df['Name'].str.strip(r'\"\.\*')

0       Tom hola
1           nick
2    krish here
3         oh my
Name: Name, dtype: object

# OR
df.Name.str.replace(r'^\W+|(.*?)\W+$',r'\1') # Replaces only values in `Name`
# df.replace(r'^\W+|(.*?)\W+$',r'\1',regex=True) Replaces for whole df
  • More about regex pattern used in second case here

use (\W)*$ if you want to match all specials characters at the end of the string

df = pd.DataFrame({'Name': ['Tom hola.', 'secondx //', 'nick', 'krish here .']})
df['Name'] = df['Name'].replace({r'(\W)*$': ""}, regex=True)

Output :

         Name
0     Tom hola
1    secondx 
2        nick
3  krish here

You can use https://regex101.com to test and better understand what your regex is doing

Related