1

I am new to pandas. I am trying to fetch a multiple substring from a string. But I need to check between particular start and end.

if it is present i need to get its position, which substring.

1
  • 1
    Can you provide a sample of the data? Commented Jan 30, 2023 at 16:08

1 Answer 1

1

Use str.replace:

target = 'hi|love'

m = df['sequence'].str.contains(target)

df.loc[m, 'output'] = (df.loc[m, 'sequence']
                         .str.replace(fr'.*({target}).*',
                                      lambda m: f'{m.start(1)+1},{m.group(1)}',
                                      regex=True)
                       )

df.loc[~m, 'output'] = 'NA'

Output:

      sequence  output
0   HelloWorld      NO
1    worldofhi    8,hi
2  worldoflove  8,love

Used input:

      sequence
0   HelloWorld
1    worldofhi
2  worldoflove

checking only in substring 7:10

target = 'hi|love'

s = df['sequence'].str[7:10+1]

m = s.str.contains(target)

df.loc[m, 'output'] = (s[m]
                         .str.replace(fr'.*({target}).*',
                                      lambda m: f'{m.start(1)+7+1},{m.group(1)}',
                                      regex=True)
                       )

df.loc[~m, 'output'] = 'NA'
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. But I need the string to be checked only from 7 to 10 in the position.
@julie then, just slice before and add 7 to the position, see update
Thank you so much. But may I know what is actually doing here s[m] .str.replace(fr'.*({target})', lambda m: f'{m.start(1)+7+1},{m.group(1)}',
When there is another sequence like "worldofhide" , the output print like "hid" instead of "hi"
Do you wan to match the end of string? What you expect is unclear, please be specific.
|

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.