adele adele - 2 months ago 14
Python Question

Modify output from series.rolling to 2 decimal points

Using the following data:

Open High Low Last Volume
Timestamp
2016-06-10 16:10:00 2088.00 2088.0 2087.75 2087.75 1418
2016-06-10 16:11:00 2088.00 2088.0 2087.75 2088.00 450
2016-06-10 16:12:00 2088.00 2088.0 2087.25 2087.25 2898


I am looking to use a rolling moving average as follows:

data["sma_9_volume"] = data.Volume.rolling(window=9,center=False).mean()


and this gives me this output:

Open High Low Last Volume candle_range sma_9_close sma_9_volume
Timestamp

2014-03-04 09:38:00 1785.50 1785.50 1784.75 1785.25 24 0.75 1785.416667 48.000000
2014-03-04 09:39:00 1785.50 1786.00 1785.25 1785.25 13 0.75 1785.500000 30.444444
2014-03-04 09:40:00 1786.00 1786.25 1783.50 1783.75 28 2.75 1785.333333 30.444444
2014-03-04 09:41:00 1784.00 1785.00 1784.00 1784.25 12 1.00 1785.083333 22.777778
2014-03-04 09:42:00 1784.25 1784.75 1784.00 1784.25 18 0.75 1784.972222 20.222222
2014-03-04 09:43:00 1784.75 1785.00 1784.50 1784.50 10 0.50 1784.888889 20.111111
2014-03-04 09:44:00 1784.25 1784.25 1783.75 1784.00 32 0.50 1784.694444 18.222222


what is the best way to take the output from:

data["sma_9_volume"] = data.Volume.rolling(window=9,center=False).mean()


and have the output only return 2 decimal points i.e.
48.00
instead of
48.000000

Answer

you can use pandas' round function

data["sma_9_volume"]=data["sma_9_volume"].round(decimals=2)

or directly:

data["sma_9_volume"] = data.Volume.rolling(window=9,center=False).mean().round(decimals=2)

documentation