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

Python regex match n characters before the keyword

I have a dataframe column looks like this

data
Membership 1 year
Individual - 10 years:
Membership 2019-2024

I want to extract the number before the keyword ‘year’.
I’d like to get output:

data                      contract_years
Membership 1 year         1
Individual - 10 years:    10
Membership 2019-2024

I tried
\d{2}+(?=year) which is obviously wrong. Any suggestion will be really helpful, thanks.

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 :

df['contract_years'] = df['data'].str.extract('(\d+) year')

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