Make sure that kilogram are converted to grams pandas
Question:
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
{0:1000,
1:500,
2:200}
Answers:
You can extract
the values/units with a short regex, then map
your units and multiply:
units = {'kg': 1000, 'g': 1}
out = (s.str.extract('(?P<value>d+)(?P<unit>D+)')
.assign(unit=lambda d: d['unit'].map(units))
.astype(int).prod(axis=1)
)
print(out)
Variant:
units = {'kg': 1000, 'g': 1}
df = s.str.extract('(d+)(D+)')
out = df[0].astype(int).mul(df[1].map(units))
handling floating point values and spaces:
units = {'kg': 1000, 'g': 1}
df = s.str.extract('(d.?d*+)s*(D+)')
out = df[0].astype(float).mul(df[1].map(units))
Output:
0 1000
1 500
2 200
dtype: int64
Setting up a basic conversion function and applying it should do the trick:
import pandas as pd
s = pd.Series({0: '1kg',
1: '500g',
2: '200g'})
def convert(weight):
if weight[-2:] == "kg":
return int(weight[:-2])*1000
elif weight[-1] == "g":
return int(weight[:-1])
else:
return int(weight)
s = s.apply(convert)
print(s)
Output:
0 1000
1 500
2 200
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
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
{0:1000,
1:500,
2:200}
You can extract
the values/units with a short regex, then map
your units and multiply:
units = {'kg': 1000, 'g': 1}
out = (s.str.extract('(?P<value>d+)(?P<unit>D+)')
.assign(unit=lambda d: d['unit'].map(units))
.astype(int).prod(axis=1)
)
print(out)
Variant:
units = {'kg': 1000, 'g': 1}
df = s.str.extract('(d+)(D+)')
out = df[0].astype(int).mul(df[1].map(units))
handling floating point values and spaces:
units = {'kg': 1000, 'g': 1}
df = s.str.extract('(d.?d*+)s*(D+)')
out = df[0].astype(float).mul(df[1].map(units))
Output:
0 1000
1 500
2 200
dtype: int64
Setting up a basic conversion function and applying it should do the trick:
import pandas as pd
s = pd.Series({0: '1kg',
1: '500g',
2: '200g'})
def convert(weight):
if weight[-2:] == "kg":
return int(weight[:-2])*1000
elif weight[-1] == "g":
return int(weight[:-1])
else:
return int(weight)
s = s.apply(convert)
print(s)
Output:
0 1000
1 500
2 200
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