@ -2707,8 +2707,8 @@ b <span class="hljs-number">3</span> <span class="hljs-number">4</span>
< / code > < / pre >
< pre > < code class = "python language-python hljs" > < DF> = < DF> .set_index(column_key) < span class = "hljs-comment" > # Replaces row keys with values from a column.< / span >
< DF> = < DF> .reset_index() < span class = "hljs-comment" > # Moves row keys to their own column.< / span >
< DF> = < DF> .transpose() < span class = "hljs-comment" > # Rotates the table .< / span >
< DF> = < DF> .melt(id_vars=column_key/s) < span class = "hljs-comment" > # Melts on columns .< / span >
< DF> = < DF> .filter(< span class = "hljs-string" > '< regex> '< / span > , axis=< span class = "hljs-number" > 1< / span > ) < span class = "hljs-comment" > # Only keeps columns whose key matches the regex .< / span >
< DF> = < DF> .melt(id_vars=column_key/s) < span class = "hljs-comment" > # Convers DF from wide to long format .< / span >
< / code > < / pre >
< div > < h4 id = "mergejoinconcat" > Merge, Join, Concat:< / h4 > < pre > < code class = "python language-python hljs" > < span class = "hljs-meta" > > > > < / span > l = DataFrame([[< span class = "hljs-number" > 1< / span > , < span class = "hljs-number" > 2< / span > ], [< span class = "hljs-number" > 3< / span > , < span class = "hljs-number" > 4< / span > ]], index=[< span class = "hljs-string" > 'a'< / span > , < span class = "hljs-string" > 'b'< / span > ], columns=[< span class = "hljs-string" > 'x'< / span > , < span class = "hljs-string" > 'y'< / span > ])
x y
@ -2871,12 +2871,12 @@ plotly.express.line(df, x=<span class="hljs-string">'Date'</span>, y=<span class
covid = pd.read_csv(< span class = "hljs-string" > 'https://covid.ourworldindata.org/data/owid-covid-data.csv'< / span > ,
usecols=[< span class = "hljs-string" > 'date'< / span > , < span class = "hljs-string" > 'total_cases'< / span > ])
covid = covid.groupby(< span class = "hljs-string" > 'date'< / span > ).sum()
dow, gold, btc = [scrape_yahoo(id_) < span class = "hljs-keyword" > for< / span > id_ < span class = "hljs-keyword" > in< / span > (< span class = "hljs-string" > '^DJI'< / span > , < span class = "hljs-string" > 'GC=F'< / span > , < span class = "hljs-string" > 'BTC-USD'< / span > )]
dow.name, gold.name, btc.name = < span class = "hljs-string" > 'Dow Jones'< / span > , < span class = "hljs-string" > 'Gold'< / span > , < span class = "hljs-string" > 'Bitcoin'< / span >
< span class = "hljs-keyword" > return< / span > covid, dow, gold, btc
dow, gold, bi tcoin = [scrape_yahoo(id_) < span class = "hljs-keyword" > for< / span > id_ < span class = "hljs-keyword" > in< / span > (< span class = "hljs-string" > '^DJI'< / span > , < span class = "hljs-string" > 'GC=F'< / span > , < span class = "hljs-string" > 'BTC-USD'< / span > )]
dow.name, gold.name, bi tcoin .name = < span class = "hljs-string" > 'Dow Jones'< / span > , < span class = "hljs-string" > 'Gold'< / span > , < span class = "hljs-string" > 'Bitcoin'< / span >
< span class = "hljs-keyword" > return< / span > covid, dow, gold, bi tcoin
< span class = "hljs-function" > < span class = "hljs-keyword" > def< / span > < span class = "hljs-title" > wrangle_data< / span > < span class = "hljs-params" > (covid, dow, gold, btc)< / span > :< / span >
df = pandas.concat([covid, dow, gold, btc], axis=< span class = "hljs-number" > 1< / span > )
< span class = "hljs-function" > < span class = "hljs-keyword" > def< / span > < span class = "hljs-title" > wrangle_data< / span > < span class = "hljs-params" > (covid, dow, gold, bi tcoin )< / span > :< / span >
df = pandas.concat([covid, dow, gold, bi tcoin ], axis=< span class = "hljs-number" > 1< / span > )
df = df.loc[< span class = "hljs-string" > '2020-02-23'< / span > :].iloc[:< span class = "hljs-number" > -2< / span > ]
df = df.interpolate()
df.iloc[:, < span class = "hljs-number" > 1< / span > :] = df.rolling(< span class = "hljs-number" > 10< / span > , min_periods=< span class = "hljs-number" > 1< / span > , center=< span class = "hljs-keyword" > True< / span > ).mean().iloc[:, < span class = "hljs-number" > 1< / span > :]