Home > Net >  only remove integers within bracket from df column - pandas
only remove integers within bracket from df column - pandas

Time:09-13

I'm trying to remove brackets with integers only from a df column. If it's an object than I'm hoping to keep it. Using below, there are certain rows that have two brackets. I only want to remove brackets that are all integers.

df = pd.DataFrame({
'col': ['Bear  (123)', 'Dog (No 2a)  (1502)', 'Cat Dog (No 7b)  (16772)', 'Bear  (123)']})

df['col'] = df['col'].str.replace(r"\(.*\)","")

intended out:

               col
0             Bear  
1      Dog (No 2a)
2  Cat Dog (No 7b)
3             Bear  

CodePudding user response:

You can use:

df['col'] = df['col'].str.replace(r'\s*\(\d*\)', '', regex=True)

output:

               col
0             Bear
1      Dog (No 2a)
2  Cat Dog (No 7b)
3             Bear

regex demo

\s*    # match spaces (optional)
\(     # match literal (
\d*    # match zero or more digits (use \d  to force at least 1 digit)
\)     # match literal )

NB. use regex=True to avoid the future warning.

CodePudding user response:

Use \s* for match optionally spaces before and after brackets, \d for match integers:

df['col'] = df['col'].str.replace(r"\s*\(\d \)\s*","", regex=True)
print (df)
               col
0             Bear
1      Dog (No 2a)
2  Cat Dog (No 7b)
3             Bear
  • Related