Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract string between two delimiters in Python dataframe

I am trying to extract values between : and - from this below

>>> all_cancers.iloc[:,3]
0        chr1:100414771-100414772
1          chr1:10506157-10506158
2        chr1:109655506-109655507
3        chr1:113903257-113903258
4        chr1:117598869-117598870

I tried re.findall('\:(.*?)\-', all_cancers.iloc[:,3].astype(str)) to do this but it generates the following error: TypeError: expected string or bytes-like object.

What is missing here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use this pattern,

In [33]: re.match(r'.*:(.*)-',"chr1:100414771-100414772").group(1)
Out[33]: '100414771'

In datafame you can do with apply + lambda

all_cancers.iloc[:,3].apply(lambda x: re.match(r'.*:(.*)-', x).group(1))

Using extract

all_cancers.iloc[:,3].str.extract(r'.*:(.*)-')

(credit: OlvinRoght’s comment)

Debuggex Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading