Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to calculate a moving average in a dataframe?

I have a dataframe column that looks like this:

CurrentCreditLines
0   5.0
1   14.0
2   NaN
3   5.0
4   19.0

with 110k records, how can I calculate the moving average? also I need it to be rounded and with type float, I tried this:

test["CurrentCreditLines"].rolling(min_periods=1, center=True, window=12).mean().round().float()

but I got the error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'Series' object has no attribute 'float'

>Solution :

The error you’re getting is telling you that it’s not an issue with your .rolling() method but there is no .float() attribute of series in pandas, so, you should use pandas.DataFrame.astype() function to manipulate column dtypes.

test["CurrentCreditLines"].rolling(min_periods=1, center=True, window=12).mean().round()

test["CurrentCreditLines"].astype(float)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading