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

Slicing numeric value from a categorical column's text

I’m working with a dataframe where one of the columns is like this:

Rating
4.8 out of 5 stars
4.0 out of 5 stars
4.5 out of 5 stars

and I want to slice this data keeping only the first number, e.g.

Rating
4.8
4.0
4.5

how can I solve it?

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 :

To extract a field from a string (or categorical) column’s text, use pandas Series.str.extract with a regex:

df['Rating'].str.extract('([1-5]\.[0-9])')

     0
0  4.8
1  4.0
2  4.5

df = pd.DataFrame({'Rating': ['4.8 out of 5 stars', '4.0 out of 5 stars', '4.5 out of 5 stars']}, dtype='category')

You can tweak that regex if you need, please see the manpage. It assumes all ratings are a decimal (not integer), and have one decimal place.

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