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

Regex to cut last one or two digits from string in dataframe

I have data like that:
App2Aa1
Cry10Aa4
Cry2Ac10

I want output like:
App2Aa
Cry10Aa
Cry2Ac

I tried regex

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

data['id'] = data['id'].str.replace(r'[a-zA-Z][0-9][0-9][a-zA-Z]\d+', r'[a-zA-Z]+[0-9][0-9]\w\w', regex=False)

My case is to Cut one or two digits and make record shorter

>Solution :

I think you want to just strip all trailing digits. If so, then we can use str.replace with the regex pattern \d+$:

data["id"] = data["id"].str.replace(r'\d+$', '', regex=True)
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