2

I have a column of a pandas dataframe (df). It looks like this:

Mese
__________
{12313}Luglio
{34}Maggio

I was trying to use regex to get rid of the {} and everything in between:

df['Mese']=[re.sub(r'^{d+}','', str(x)) for x in df['Mese']]

It does not work. Any help?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
coelidonum
  • 523
  • 5
  • 17

1 Answers1

1

You need to use Series.str.replace with ^\{\d+} pattern:

 df['Mese'] = df['Mese'].str.replace(r'^\{\d+}', '')

Pandas test:

>>> import pandas as pd
>>> df = pd.DataFrame({'Mese':['{12313}Luglio','{34}Maggio']})
>>> df['Mese'] = df['Mese'].str.replace(r'^\{\d+}', '')
>>> df
     Mese
0  Luglio
1  Maggio

The ^\{\d+} pattern matches a number inside curly braces at the start of the string.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563