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 – How to split a Pandas value and only get the value between the slashs

Example:
I the df['column'] has a bunch of values similar to: F/4500/O or G/2/P

The length of the digits range from 1 to 4 similar to the examples given above.

How can I transform that column to only keep 1449 as an integer?

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

I tried the split method but I can’t get it right.
Thank you!

enter image description here

>Solution :

You could extract the value and convert to_numeric:

df['number'] = pd.to_numeric(df['column'].str.extract('/(\d+)/', expand=False))

Example:

     column  number
0  F/4500/O    4500
1     G/2/P       2
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