Home > front end >  Converting all rows in a pandas dataframe column from scientific notation to float
Converting all rows in a pandas dataframe column from scientific notation to float

Time:10-05

I am trying to convert all rows in one column from a scientific notation to a float. I am trying this solution below but getting a syntax error. Does anyone have a better way of doing it?

future = model.make_future_dataframe(periods=15,freq='M')
forecast = model.predict(future)
forecast.tail(15)

forecast['yhat'] = df.apply({:.7f}.format(forecast['yhat']), axis = 1)

CodePudding user response:

to force a display as decimal using set_option

pd.set_option('display.float_format', '{:.7f}'.format)
df = pd.DataFrame({
    'Name': ['a', 'b', 'c'],
    'Value': np.random.rand(3)**22
})
df


    Name    Value
0   a   3.803715e-02
1   b   3.056442e-03
2   c   4.176117e-10

pd.set_option('display.float_format', '{:.7f}'.format)

    Name    Value
0   a   0.0380371
1   b   0.0030564
2   c   0.0000000


# reset the format option
pd.reset_option('display.float_format')
  • Related