Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

12 month movinf average in python for dataframe

start_date = '2015-01-01'
end_date = '2016-12-31'

fig, ax = plt.subplots(figsize=(16,9))

ax.plot(data.loc[start_date:end_date, :].index, data.loc[start_date:end_date, 'MSFT'], label='Price')
ax.plot(long_rolling.loc[start_date:end_date, :].index, long_rolling.loc[start_date:end_date, 'MSFT'], label = '100-days SMA')
ax.plot(short_rolling.loc[start_date:end_date, :].index, short_rolling.loc[start_date:end_date, 'MSFT'], label = '20-days SMA')

ax.legend(loc='best')
ax.set_ylabel('Price in $')
ax.xaxis.set_major_formatter(my_year_month_fmt)
Comment

12 month movinf average in python for dataframe

# Calculating the long-window simple moving average
long_rolling = data.rolling(window=100).mean()
long_rolling.tail()
Comment

12 month movinf average in python for dataframe

# Calculating the short-window simple moving average
short_rolling = data.rolling(window=20).mean()
short_rolling.head(20)
Comment

12 month movinf average in python for dataframe

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib inline
import seaborn as sns
sns.set(style='darkgrid', context='talk', palette='Dark2')

my_year_month_fmt = mdates.DateFormatter('%m/%y')

data = pd.read_pickle('./data.pkl')
data.head(10)
Comment

PREVIOUS NEXT
Code Example
Python :: qtimer singleshot 
Python :: python game github 
Python :: Customizing scatter plot with pyplot object 
Python :: async asyncio input 
Python :: Access Google Photo API with Python using google-api-python-client 
Python :: format number differences in python 
Python :: python not equal to 
Python :: python re.findall() 
Python :: get variable from function python 
Python :: how to open youtube from google chrome browser instead of internet explorerwhen coding in python 
Python :: python if file exist 
Python :: dataframe rolling window 
Python :: python telegram bot async 
Python :: networkx node attribute from a dataframe 
Python :: Common Python String Methods 
Python :: select column in pandas dataframe 
Python :: django change id to uuid 
Python :: Get text without inner tags text in selenium 
Python :: loop through dataframe rows python 
Python :: pandas series map 
Python :: Merge two Querysets in Python Django while preserving Queryset methods 
Python :: target encoder sklearn example 
Python :: insert-cells-in-empty-pandas-dataframe 
Python :: python vector class 
Python :: update django model with dict 
Python :: save jupyter notebook session 
Python :: how to add array and array in python 
Python :: how to know the version of python 
Python :: concatenacion python 
Python :: python convert string to float 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =