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

Make sure that kilogram are converted to grams pandas

I have the following series

s = pd.Series({0: '1kg',
 1: '500g',
 2: '200g'})

What I want to do is to make a very similar column that basically has the same type of measurement, that being in grams. So in this case convert the 1kg to one thousand int value and leave the gram integer in the normal state. Note: The value can alter on the kg part. Any ideas on how I could do this?

Wanted result

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

{0:1000,
1:500,
2:200}

>Solution :

The module quantiphy handles this type of use case:

import pandas as pd 
from quantiphy import Quantity

s = pd.Series({0: '1kg',
 1: '500g',
 2: '200g'})

print(s.apply(Quantity))

Output:

0    1000.0
1     500.0
2     200.0
dtype: float64
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