Python Forum
Newbie question on how to use pandas.rolling_mean
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie question on how to use pandas.rolling_mean
#1
import pandas as pd

data = [1.0, 2.0, 3.0, 4.0]
rollmean_data_2 = pd.rolling_mean(data, 2)
print(rollmean_data_2)
I want to learn how to use rolling_mean by pandas, the pandas version is 0.21.0.
But when I run the above code, I got the following error:
AttributeError: 'list' object has no attribue 'rolling'
Please show me how to use pandas.rolling_mean
Or if other python package has the similar function, please also advise how to use them.
Thanks,
Reply
#2
http://pandas.pydata.org/pandas-docs/ver..._mean.html
Docs suggest that your data argument must be of type Series or DataFrame. List is not accepted, which is what your error message tells.
Reply
#3
import pandas as pd

data = [1.0, 2.0, 3.0, 4.0]
s1 = pd.Series(data)
rollmean = pd.rolling_mean(s1, 2)
print(rollmean)
Hello, I changed my code according to your advice, but I got some warning:
Warning: pd.rolling_mean is deprecated for Series and will be removed in a future version, replace with
Series.rolling(window=2, center=False).mean()

However, I did see the result:
0 NaN
1 1.5
2 2.5
3 3.5
dtype: float64

I want to know how to use the new version or the version in future to do the same job.
Another issue is: the first value is: NaN
How I can either delete the first value or replace the value NaN by another value, i.e. 0.0
Thanks,
Reply
#4
Warning tells you which function to use instead of rolling_mean. Replace it with Series.rolling. Search online or check in docs to find out how to use it. If you won't be able to get it to work, post again with code and error message.
About removing NaNs or replacing them with another value, it's very easy to find answer to that using your favourite search engine. So again, try it and post back if it won't work.
Reply
#5
from numpy import *
import numpy as np
import pandas as pd

data = [1.0, 2.0, 3.0, 4.0]
s1 = pd.Series(data)
rollmean = pd.Series.rolling(s1, 2).mean()
#print(rollmean)
filled0 = [0.0 if isnan(item) else item for item in rollmean]
print(filled0)
Great, I have figured it out with the above code.
Thanks,
Reply
#6
I'm glad you got it working!
Check out this function which is already impelemented, you may find it a more elegant solution ;)
https://pandas.pydata.org/pandas-docs/st...illna.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas df inside a df question mbaker_wv 4 1,179 Dec-25-2022, 01:11 AM
Last Post: mbaker_wv
  Pandas usecols question rsearing 1 1,234 Aug-20-2022, 10:10 PM
Last Post: jefsummers
  Simple pandas question mcva 4 2,637 Dec-17-2021, 04:47 PM
Last Post: mcva
  Pandas question new2datasci 0 1,941 Jan-10-2021, 01:29 AM
Last Post: new2datasci
  Pandas merge question smw10c 2 5,706 Jul-02-2020, 06:56 PM
Last Post: hussainmujtaba
  Counting Criteria in Pandas Question Koenig 1 2,155 Sep-30-2019, 05:16 AM
Last Post: perfringo
  Function question using Pandas smw10c 7 7,072 Feb-12-2019, 06:52 PM
Last Post: Nathandsn
  Simple pandas dataframe question popohoma 1 3,538 Jan-03-2019, 05:00 PM
Last Post: ashlardev
  question on pandas datareader kit12_31 3 9,204 Feb-05-2018, 11:55 PM
Last Post: snippsat
  Newbie question to return only the index of a dataframe zydjohn 0 2,551 Jan-22-2018, 03:40 PM
Last Post: zydjohn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020