I have a dataframe that has a column length
(numeric) and another column units (string). For example,
df2convert =
length units
1.0 "m" # meters
1.4 "in" # inches
0.5 "km" # kilometers
....
I have another dataframe that maps all the units to meters. For example
units2meters:
units scaling
"m" 1.0
"in" 0.0254
"km" 1000
I want to convert everything in df2convert
to meters.
Currently, I do it without joining/merging the 2 dataframes.
I convert units2meters
to a dict
with key as units
and value as scaling
and then pass in df2.convert.units
as a key into the hash table.
Is there a better/more efficient way to do this?
CodePudding user response:
Not sure whether or not this is the method you're already using, but I would use:
lookup = units2meters.set_index("units")["scaling"]
df2convert["length"] * df2convert["units"].map(lookup)
0 1.00000
1 0.03556
2 500.00000
dtype: float64
CodePudding user response:
import pandas as pd
df2convert = pd.DataFrame(data={
"length":[1.0, 1.4, 0.5],
"units":["m","in","km"]})
units2meters = pd.DataFrame(data={
"units":["m","km","in"],
"scaling":[1.0, 1000, 0.0254]})
merge = pd.merge(units2meters, df2convert, on="units")
merge["result"] = merge["length"] * merge["scaling"]