Home > other >  Pandas: df.index.str.extract() not working like re.findall()
Pandas: df.index.str.extract() not working like re.findall()

Time:10-05

I am trying to use df.<column_name or index>.str.extract(r'\d ') on the following:

28    USD-ABC-S__7y
29    USD-ABC-S__8y
Name: InstrumentIdentifier, dtype: object

This is giving me a ValueError though:

ValueError: pattern contains no capture groups

However, when I try:

import re
re.findall(r'\d ', 'USD-ABC-S__8y')

this returns the expected result: [8]

Any ideas why the Pandas version isn't giving the same result as re?

Thx

CodePudding user response:

You need to wrap the regex in a capturing group ((...)):

df.<column_name or index>.str.extract(r'(\d )')

Example:

df['InstrumentIdentifier'].str.extract(r'(\d )', expand=False)

output:

28    7
29    8
Name: InstrumentIdentifier, dtype: object
  • Related