Jason Ching Yuk Jason Ching Yuk - 3 months ago 19
Python Question

Extract after equal sign

just started python pandas on Juypter. I am having difficulties trying to get the values after the equal sign. Can anyone help me through?

Hi = 123456
123456 (result i wish to get)


My current code below returns "= 123456".

df['column'].str.extract("Hi+\W(.*)",expand= True)

Answer

You used a + quantifier on a symbol i making it match 1 or more times, thus \W matches the space after Hi.

You can use

r"Hi *[^\w ] *(.*)"

where:

  • Hi - matches Hi
  • * - matches 0 or more spaces
  • [^\w ] - a non-word char except a space
  • * - matches 0 or more spaces
  • (.*) - 0+ chars other than a newline, as many as possible (the rest of the line).

If the space after Hi is obligatory, use + quantifier r"Hi +[^\w ] *(.*)".

However, the best way is to use a mere split: 'Hi = 123456'.split(' = ') (demo)

Comments