Last active
January 21, 2020 09:41
-
-
Save whiledoing/d7155e21aad0c15f1d7ef75e9159f63c to your computer and use it in GitHub Desktop.
[pandas-snippet] pandas snippets #python #pandas #datascience
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pandas_datareader import data | |
goog = data.DataReader('GOOG', start='2014', end='2020', data_source='yahoo') | |
goog.columns = goog.columns.str.lower() | |
goog = goog.asfreq('D', method='bfill') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib import rcParams | |
from matplotlib.font_manager import findfont, FontProperties | |
rcParams['font.family'] = 'sans-serif' | |
rcParams['font.sans-serif'] = [ | |
u'Microsoft Yahei', | |
u'Heiti SC', | |
u'Heiti TC', | |
u'STHeiti', | |
u'WenQuanYi Zen Hei', | |
u'WenQuanYi Micro Hei', | |
u"文泉驿微米黑", | |
u'SimHei', | |
] | |
rcParams['axes.unicode_minus'] = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
goog.close.plot(alpha=0.5, style='-', figsize=(12, 6)); | |
goog.close.asfreq('BA').plot(style='--o'); | |
goog.close.resample('BA').mean().plot(style=':o'); | |
plt.legend(['input', 'asfreq', 'resample'], loc='upper left'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rolling = goog.close.rolling(365, center=True) | |
data = pd.DataFrame({ | |
'input': goog.close, | |
'one-year rolling-mean': rolling.mean(), | |
'one-year rolling-std': rolling.std() | |
}) | |
ax = data.plot(style=['-', '--', ':'], figsize=(12, 6)); | |
ax.lines[0].set_alpha(0.3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment