0

Just wondering what I am doing wrong with regex(very new to it).

df
             tradingsymbol
0            USDEUR18D21P
1            KNIFY20DEC182700P

I am trying to extract all characters before the first digit. And extract the part before it into another column.

I have tried:

df['tradingsymbol'].str.extract('^(\D)\d', expand=True)

It gives me all NaN result.

I have tried a few other expressions and all have either returned NaN or given an error about unbalance parenthesis.

How do I go about this?

Thanks in advance.

Expected o/p

 df
             tradingsymbol          symbol
0            USDEUR18D21P           USDEUR
1            KNIFY20DEC182700P      KNIFY
Sid
  • 2,749
  • 3
  • 21
  • 40

1 Answers1

1

You're only capturing a single non-digit with \D. You need a greedy modifier:

^(\D+)\d
Tim
  • 2,316
  • 1
  • 9
  • 26