-1

I have a string where I need to keep two key numbers and remove the rest which need to be converted to a string that can be used to slice data.

sample = 'range(0, 286)range(300, 511)'

I will always need the first two numbers (ex. 0 and 286). Each of these numbers will not always be 0 and 286. They can have multiple positions like 10 and 1000 or 100 and 10000. They will always be in parentheses and have a comma to separate each number. The second set of numbers do not need to be extracted from the string.

Expected Output: My end product would be a string that looks like a slice:

print([sample])
[0:286]

How do I extract just the first two numbers from this text, zero and two-hundred-eighty-six?

Starbucks
  • 1,448
  • 3
  • 21
  • 49
  • what is your expected output? – Sabil Aug 24 '21 at 16:58
  • `f"""{[sample[sample.find("(")+1:sample.find(")")].replace(' ','')]}"""`, addressed here: https://stackoverflow.com/questions/4894069/regular-expression-to-return-text-between-parenthesis – anky Aug 24 '21 at 17:03

1 Answers1

-1

Assuming the following input:

series = pd.Series(['range(0, 286)range(300, 511)', 'range(100, 1000)range(300, 511)'])

you can use str.extract with named groups:

series.str.extract('range\((?P<number1>\d+),\s+(?P<number2>\d+)\)')

output:

  number1 number2
0       0     286
1     100    1000
mozway
  • 194,879
  • 13
  • 39
  • 75