-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPandas 3.30 - Session 1.py
128 lines (73 loc) · 2.66 KB
/
Pandas 3.30 - Session 1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Correlation exercise
# Import libraries and dependencies
import pandas as pd
from pathlib import Path
import seaborn as sns
%mathplotlib inline
# Read the ice cream slaes data, set the 'month' as the index
ice_cream_data = Path"../____/ice_cream.csv"
#
# Create seaborn heat map
sns.heatmap(Correlation, vmin=-1, vmax=1)
pd
###Instructur exercise #2: Rolling averages
# Import libraries and dependencies
import pandas as pd
from pathlib import Path
%mathplotlib inline
#Read the TSLA data, set the date as the index, sort in forward chronological order
trsla
#### Student exercise #2
import pandas as pd
from pathlib import Path
%mathplot inline
# set the path
nflx_data = Path("../Resources/nflx.csv")
# Read the CSV into a datafrmae and set the date as datatime index
nflx_df = pd.read_csv(nflx_data, index_col="date", infer_datetime_format=True, parse_dates=True)
nflx_df.sort_index() #sort index
nflx_df.plot() # plot data frame
nflx_df.plot(type=Scatter)**** #Create a scatter plot
# set figure of the daily closing prices of NFLX
axie = nflx_df.plot(figsize(25,10))
### Instructer demo: Beta
#import libraries and dependencies
#read CSVs in the dataframs
#Calculate covariance of all daily retursn of AMZon vs. S&P 500
covariance = daily_retuns['AMZN'].cov(daily_returns['S&P 500'])
covariance
# calculate vaiance of all retursn of AMZOn vs. S&P 500
variance = dailiy_returns['S&P 500']***
# Calc. 30-day rolling beta of amzn and plot the data
rolling_beta = rolling_covarince / rolling_variance***
rolling_beta.plot(figsize=(20, 100)
# Beta vs. Correlation
import seaborn as sns
sns.lmplot(x="S&P 500", y='AMZN', data=daily_returns, aspect=1.5, fit_reg=True)
###TA Exercise Lesson #4
# Calculate covariance of all daily returns of social meda
####Instructer Exercise #3 Portfolio Returns
#libraries & dependencies
#Read CSV file
#Create a new pivot table
all_prices = pd.concat([amd], ___)
#Portfolio Returns
all_returns = all_prices.pct_change()
all.returns.head()
amd_weight = 0.5
mu_weight = 0.5
portfolio_returns = amd_weight * all_returns["AMD"] + mu_weight * all_retuns["MU"]
portfolio_returns.head()
weights = [0.5, 0.5]
portfolio_returns = all_returns.dot(weights)
portfolio_retruns.head()
####TA Demo: Student Activeity: Portfolio Planner, Pt. 1
#Import pandas & dependencies
import pandas as pd
from pathlib as Path
# import CSVs
# create data frames
bk_df = pm.read_csv(nk_data)....
#Combine DataFrames, sort index and rename Columns
combined_df = pd.conca(nk_df, fang_df, jnj_df, luv_df, mu_df, nke_df, sbux_df, t_du, wdc_df, wrk_df], axis="coluns", join="inner")
####TA Demo: Student Activeity: Portfolio Planner, Pt. 2