Search code examples
pythonpandasfractions

Convert fractions stored as characters to float64


Say we have this df:

 df = pd.DataFrame({
            'value': ['18 4/2', '2 2/2', '8.5'],
            'country': ['USA', 'Canada', 'Switzerland']
    })

Out:

        value   country
    0   18 4/2  USA
    1   2 2/2   Canada
    2   8.5     Switzerland

Note the 'value' column stores an object type:

df.dtypes

Out:

value      object
country    object
dtype: object

My question: how do we convert 'value' to decimal, while also changing the data type to float64? Note that one value (8.5) is already a decimal, and should be kept so. Desired output:

desired_output = pd.DataFrame({
        'value': [20, 3, 8.5],
        'country': ['USA', 'Canada', 'Switzerland']
})


    value   country
0   20.0    USA
1   3.0     Canada
2   8.5     Switzerland


desired_output.dtypes

value       float64
country     object
dtype: object

Solution

  • you can replace the space with the sign + and then apply eval

    print(df['value'].str.replace(' ', '+').apply(eval))
    0    20.0
    1     3.0
    2     8.5
    Name: value, dtype: float64
    

    or using pd.eval

    df['value'] = pd.eval(df['value'].str.replace(' ', '+')).astype(float)
    print(df)
      value      country
    0  20.0          USA
    1   3.0       Canada
    2   8.5  Switzerland