Browse Source

Pandas

pull/57/head
Jure Šorn 4 years ago
parent
commit
0fa07e7359
2 changed files with 14 additions and 14 deletions
  1. 14
      README.md
  2. 14
      index.html

14
README.md

@ -3181,8 +3181,8 @@ b 3 4
```python ```python
<DF> = <DF>.set_index(column_key) # Replaces row keys with values from a column. <DF> = <DF>.set_index(column_key) # Replaces row keys with values from a column.
<DF> = <DF>.reset_index() # Moves row keys to their own column. <DF> = <DF>.reset_index() # Moves row keys to their own column.
<DF> = <DF>.transpose() # Rotates the table.
<DF> = <DF>.melt(id_vars=column_key/s) # Melts on columns.
<DF> = <DF>.filter('<regex>', axis=1) # Only keeps columns whose key matches the regex.
<DF> = <DF>.melt(id_vars=column_key/s) # Convers DF from wide to long format.
``` ```
#### Merge, Join, Concat: #### Merge, Join, Concat:
@ -3387,12 +3387,12 @@ def scrape_data():
covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv', covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
usecols=['date', 'total_cases']) usecols=['date', 'total_cases'])
covid = covid.groupby('date').sum() covid = covid.groupby('date').sum()
dow, gold, btc = [scrape_yahoo(id_) for id_ in ('^DJI', 'GC=F', 'BTC-USD')]
dow.name, gold.name, btc.name = 'Dow Jones', 'Gold', 'Bitcoin'
return covid, dow, gold, btc
dow, gold, bitcoin = [scrape_yahoo(id_) for id_ in ('^DJI', 'GC=F', 'BTC-USD')]
dow.name, gold.name, bitcoin.name = 'Dow Jones', 'Gold', 'Bitcoin'
return covid, dow, gold, bitcoin
def wrangle_data(covid, dow, gold, btc):
df = pandas.concat([covid, dow, gold, btc], axis=1)
def wrangle_data(covid, dow, gold, bitcoin):
df = pandas.concat([covid, dow, gold, bitcoin], axis=1)
df = df.loc['2020-02-23':].iloc[:-2] df = df.loc['2020-02-23':].iloc[:-2]
df = df.interpolate() df = df.interpolate()
df.iloc[:, 1:] = df.rolling(10, min_periods=1, center=True).mean().iloc[:, 1:] df.iloc[:, 1:] = df.rolling(10, min_periods=1, center=True).mean().iloc[:, 1:]

14
index.html

@ -2707,8 +2707,8 @@ b <span class="hljs-number">3</span> <span class="hljs-number">4</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;DF&gt; = &lt;DF&gt;.set_index(column_key) <span class="hljs-comment"># Replaces row keys with values from a column.</span> <pre><code class="python language-python hljs">&lt;DF&gt; = &lt;DF&gt;.set_index(column_key) <span class="hljs-comment"># Replaces row keys with values from a column.</span>
&lt;DF&gt; = &lt;DF&gt;.reset_index() <span class="hljs-comment"># Moves row keys to their own column.</span> &lt;DF&gt; = &lt;DF&gt;.reset_index() <span class="hljs-comment"># Moves row keys to their own column.</span>
&lt;DF&gt; = &lt;DF&gt;.transpose() <span class="hljs-comment"># Rotates the table.</span>
&lt;DF&gt; = &lt;DF&gt;.melt(id_vars=column_key/s) <span class="hljs-comment"># Melts on columns.</span>
&lt;DF&gt; = &lt;DF&gt;.filter(<span class="hljs-string">'&lt;regex&gt;'</span>, axis=<span class="hljs-number">1</span>) <span class="hljs-comment"># Only keeps columns whose key matches the regex.</span>
&lt;DF&gt; = &lt;DF&gt;.melt(id_vars=column_key/s) <span class="hljs-comment"># Convers DF from wide to long format.</span>
</code></pre> </code></pre>
<div><h4 id="mergejoinconcat">Merge, Join, Concat:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </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>]) <div><h4 id="mergejoinconcat">Merge, Join, Concat:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </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 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>, 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>]) usecols=[<span class="hljs-string">'date'</span>, <span class="hljs-string">'total_cases'</span>])
covid = covid.groupby(<span class="hljs-string">'date'</span>).sum() 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, bitcoin = [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, bitcoin.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, bitcoin
<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, bitcoin)</span>:</span>
df = pandas.concat([covid, dow, gold, bitcoin], 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.loc[<span class="hljs-string">'2020-02-23'</span>:].iloc[:<span class="hljs-number">-2</span>]
df = df.interpolate() 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>:] 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>:]

Loading…
Cancel
Save