remove characters from pandas column

22,221

Working example

df = pd.DataFrame(dict(location=['(hello)']))

print(df)

  location
0  (hello)

@Psidom's Solution
str.strip

df.location.str.strip('()')

0    hello
Name: location, dtype: object

Option 2
str.extract

df.location.str.extract('\((.*)\)', expand=False)

0    hello
Name: location, dtype: object

Option 3
str.replace

df.location.str.replace('\(|\)', '')

0    hello
Name: location, dtype: object

Option 4
replace

df.location.replace('\(|\)', '', regex=True)

0    hello
Name: location, dtype: object
Share:
22,221
Keenan Burke-Pitts
Author by

Keenan Burke-Pitts

Updated on May 04, 2020

Comments

  • Keenan Burke-Pitts
    Keenan Burke-Pitts about 4 years

    I'm trying to simply remove the '(' and ')' from the beginning and end of the pandas column series. This is my best guess so far but it just returns empty strings with () intact.

    postings['location'].replace('[^\(.*\)?]','', regex=True)
    

    The column looks like this: screenshot of jupyter notebook