简体   繁体   中英

Python add moving average to data frame

I have a fairly large data frame in python and I am trying to add a new column for moving average. I can add the moving average for the table as a whole, the problem is I need to do multiple moving averages based off of the values in column b. For example:

Column B          Column C
1                 4
2                 5
3                 6 
1                 7
2                 8 
3                 9

so the new column would have the moving averages for 1,2,3,....

Thanks for the help.

MA_WINDOW = 2
data = ('C:\\Users\\Benjamin\\Desktop\\XSO.csv') 
df = pd.read_csv(data, names=['Indicie', 'Date', 'Open', 'High', 'Low','Close', 'Vol', 'MA'])
df['MA'] = df.Close.rolling(window=MA_WINDOW).mean()
print(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM