0

I have a column in a dataframe which have a pattern as shown in the below example

id=1;name=x;color=blue;

I am trying to extract the value for name and store it in the same dataframe with column name as "name" and value is "x"

I tried the below code but it doesn't seem to work correctly

df['name'] = df['col'].str.extract("'name': '(.*?)'")
1
  • There are no quotes in the string, this will never match name': '(.*?)' Commented Mar 9, 2022 at 14:32

1 Answer 1

1

The 'name': '(.*?)' regex is meant to extract a string between 'name': ' and the next ' char. You do not have single quotes, nor : in your input.

You can use

df['name'] = df['col'].str.extract(r"name=([^;]*)")

See the regex demo, name=([^;]*) matches the name= as left-hand side context and extracts one or more chars other than a ; right after that into the name column.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.