1

I have a dataframe with the string column as:

df['C23']

Col1 Col2
11   /*[lion]*/
21   /*[tiger]*/

I need the following:

Col1 Col2
11   lion
21   tiger

I tried the following code:

df['C23'].str.extract(r"/*(.*?)*/')

but it produces empty strings.

1
  • I am confused, is the column C23 or Col2? is 11 /*[lion]*/ the content of the column? Commented Sep 28, 2021 at 15:06

2 Answers 2

1

You can use

df['result'] = df['C23'].str.extract(r"/\*\[(.*?)]\*/")

The /\*\[(.*?)]\*/ regex matches

  • /\*\[ - /*[ string
  • (.*?) - Group 1: any zero or more chars other than line break chars as few as possible
  • ]\*/ - ]*/ string
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you want to convert /*[lion]*/ to lion and all elements follow the same pattern, you do not need a regex, just slice:

df['Col2'] = df['Col2'].str[3:-3]

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.