Mukul Mukul - 4 months ago 7
Python Question

how to change default precision in python

i have a dataframe with floating type variables, i need to change the values of that variable to a n decimal places. (dummy data)

prod_code sales_avg
122 12332.234233
123 12212.234123


Currently i am doing the following :

prod_sales_avg=pd.DataFrame(weekly_sales.groupby(['prod_code']).agg({'weekly_sales':{'sales_avg':'mean'}}))
prod_sales_avg.columns=prod_sales_avg.columns.droplevel(0)
prod_sales_avg=prod_sales_avg.reset_index()
prod_sales_avg['sales_avg']=round(prod_sales_avg['sales_avg'],9)


i am getting the decimal places upto 6 places(i think that is default). I need to increase the decimal place to 9.but the above code does not work. Later i will be using this variable for comparison, so not looking for just "display" standpoint. The value should be stored as 9 decimal places. What am i doing wrong here.

Answer

You can do it as follows:

via Dataframe using Dataframe.round

prod_sales_avg.round({'sales_avg': 9})

via Series using Series.round

prod_sales_avg['sales_avg'] = prod_sales_avg['sales_avg'].round(decimals=9)