Skip to content

Commit

Permalink
[ci skip] deploy from Chankey
Browse files Browse the repository at this point in the history
  • Loading branch information
werckerbot committed May 11, 2017
1 parent 4d19f4e commit fe1cf56
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 19 deletions.
9 changes: 9 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,12 @@ i.freebsd-19px:before {



img[src$='#reducesize']
{
width: 90%;
}

img[src$='#floatright']
{
float:right;
}
Binary file added img/plot_dataframe.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/plot_single_column.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 95 additions & 6 deletions index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<link>http://tutswiki.com/index.xml</link>
<description>Recent content on </description>
<generator>Hugo -- gohugo.io</generator>
<lastBuildDate>Wed, 10 May 2017 00:00:00 +0000</lastBuildDate>
<lastBuildDate>Thu, 11 May 2017 00:00:00 +0000</lastBuildDate>
<atom:link href="http://tutswiki.com/index.xml" rel="self" type="application/rss+xml" />

<item>
<title>Chapter 1 - Reading from a CSV</title>
<link>http://tutswiki.com/pandas-cookbook/chapter1</link>
<pubDate>Wed, 10 May 2017 00:00:00 +0000</pubDate>
<pubDate>Thu, 11 May 2017 00:00:00 +0000</pubDate>

<guid>http://tutswiki.com/pandas-cookbook/chapter1</guid>
<description>
Expand Down Expand Up @@ -42,11 +42,11 @@ print broken_df[:3]
&lt;p&gt;You&amp;rsquo;ll notice that this is totally broken! read_csv has a bunch of options that will let us fix that, though. Here we&amp;rsquo;ll&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the column separator to a ;&lt;/li&gt;
&lt;li&gt;Set the encoding to &amp;lsquo;&lt;em&gt;latin1&lt;/em&gt;&amp;rsquo; (the default is &amp;lsquo;&lt;em&gt;utf8&lt;/em&gt;&amp;rsquo;)&lt;/li&gt;
&lt;li&gt;Parse the dates in the &amp;lsquo;Date&amp;rsquo; column&lt;/li&gt;
&lt;li&gt;Change the column separator to a &lt;code&gt;;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Set the encoding to &lt;code&gt;&#39;_latin1_&#39;&lt;/code&gt; (the default is &lt;code&gt;&#39;_utf8_&#39;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Parse the dates in the &lt;code&gt;&#39;Date&#39;&lt;/code&gt; column&lt;/li&gt;
&lt;li&gt;Tell it that our dates have the date first instead of the month first&lt;/li&gt;
&lt;li&gt;Set the index to be the &amp;lsquo;Date&amp;rsquo; column&lt;/li&gt;
&lt;li&gt;Set the index to be the &lt;code&gt;&#39;Date&#39;&lt;/code&gt; column&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;fixed_df = pd.read_csv(&#39;bikes.csv&#39;, sep=&#39;;&#39;, encoding=&#39;latin1&#39;, parse_dates=[&#39;Date&#39;], dayfirst=True, index_col=&#39;Date&#39;)
Expand Down Expand Up @@ -112,6 +112,95 @@ print fixed_df[:3]
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&#34;1-2-selecting-a-column&#34;&gt;1.2 Selecting a column&lt;/h2&gt;

&lt;p&gt;When you read a CSV, you get a kind of object called a &lt;a href=&#34;http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html&#34;&gt;DataFrame&lt;/a&gt;, which is made up of rows and columns. You get columns out of a DataFrame the same way you get elements out of a dictionary.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s an example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;print fixed_df[&#39;Berri 1&#39;]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;Date
2012-01-01 35
2012-01-02 83
2012-01-03 135
2012-01-04 144
2012-01-05 197
2012-01-06 146
2012-01-07 98
2012-01-08 95
2012-01-09 244
2012-01-10 397
2012-01-11 273
2012-01-12 157
2012-01-13 75
2012-01-14 32
2012-01-15 54
...
2012-10-22 3650
2012-10-23 4177
2012-10-24 3744
2012-10-25 3735
2012-10-26 4290
2012-10-27 1857
2012-10-28 1310
2012-10-29 2919
2012-10-30 2887
2012-10-31 2634
2012-11-01 2405
2012-11-02 1582
2012-11-03 844
2012-11-04 966
2012-11-05 2247
Name: Berri 1, Length: 310, dtype: int64
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;1-3-plotting-a-column&#34;&gt;1.3 Plotting a column&lt;/h2&gt;

&lt;p&gt;Just add &lt;code&gt;.plot()&lt;/code&gt; to the end! How could it be easier? =)&lt;/p&gt;

&lt;p&gt;We can see that, unsurprisingly, not many people are biking in January, February, and March.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;import pandas as pd
import matplotlib.pyplot as plt

fixed_df = pd.read_csv(&#39;bikes.csv&#39;, sep=&#39;;&#39;, encoding=&#39;latin1&#39;, parse_dates=[&#39;Date&#39;], dayfirst=True, index_col=&#39;Date&#39;)
fixed_df[&#39;Berri 1&#39;].plot()
plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:
&lt;div&gt;
&lt;img src=&#34;http://tutswiki.com/img/plot_single_column.png&#34; alt=&#34;Plotting CSV column with Matplotlib&#34; /&gt;
&lt;/div&gt;
We can also plot all the columns just as easily. We&amp;rsquo;ll make it a little bigger, too. You can see that it&amp;rsquo;s more squished together, but all the bike paths behave basically the same &amp;ndash; if it&amp;rsquo;s a bad day for cyclists, it&amp;rsquo;s a bad day everywhere.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;fixed_df.plot(figsize=(15, 10))
plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;div&gt;
&lt;img src=&#34;http://tutswiki.com/img/plot_dataframe.png&#34; alt=&#34;Plotting Dataframe with Matplotlib&#34; /&gt;
&lt;/div&gt;

&lt;h2 id=&#34;1-4-putting-all-that-together&#34;&gt;1.4 Putting all that together&lt;/h2&gt;

&lt;p&gt;Here&amp;rsquo;s the code we needed to write do draw that graph, all together:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;df = pd.read_csv(&#39;bikes.csv&#39;, sep=&#39;;&#39;, encoding=&#39;latin1&#39;, parse_dates=[&#39;Date&#39;], dayfirst=True, index_col=&#39;Date&#39;)
df[&#39;Berri 1&#39;].plot()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:
&lt;div&gt;
&lt;img src=&#34;http://tutswiki.com/img/plot_single_column.png&#34; alt=&#34;Plotting CSV column with Matplotlib&#34; /&gt;
&lt;/div&gt;&lt;/p&gt;
</description>
</item>

Expand Down
99 changes: 94 additions & 5 deletions pandas-cookbook/chapter1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ <h2 id="1-1-reading-data-from-a-csv-file">1.1 Reading data from a CSV file</h2>
<p>You&rsquo;ll notice that this is totally broken! read_csv has a bunch of options that will let us fix that, though. Here we&rsquo;ll</p>

<ul>
<li>Change the column separator to a ;</li>
<li>Set the encoding to &lsquo;<em>latin1</em>&rsquo; (the default is &lsquo;<em>utf8</em>&rsquo;)</li>
<li>Parse the dates in the &lsquo;Date&rsquo; column</li>
<li>Change the column separator to a <code>;</code></li>
<li>Set the encoding to <code>'_latin1_'</code> (the default is <code>'_utf8_'</code>)</li>
<li>Parse the dates in the <code>'Date'</code> column</li>
<li>Tell it that our dates have the date first instead of the month first</li>
<li>Set the index to be the &lsquo;Date&rsquo; column</li>
<li>Set the index to be the <code>'Date'</code> column</li>
</ul>

<pre><code class="language-python">fixed_df = pd.read_csv('bikes.csv', sep=';', encoding='latin1', parse_dates=['Date'], dayfirst=True, index_col='Date')
Expand Down Expand Up @@ -215,12 +215,101 @@ <h2 id="1-1-reading-data-from-a-csv-file">1.1 Reading data from a CSV file</h2>
</tbody>
</table>

<h2 id="1-2-selecting-a-column">1.2 Selecting a column</h2>

<p>When you read a CSV, you get a kind of object called a <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html">DataFrame</a>, which is made up of rows and columns. You get columns out of a DataFrame the same way you get elements out of a dictionary.</p>

<p>Here&rsquo;s an example:</p>

<pre><code class="language-python">print fixed_df['Berri 1']
</code></pre>

<p>Output:</p>

<pre><code class="language-bash">Date
2012-01-01 35
2012-01-02 83
2012-01-03 135
2012-01-04 144
2012-01-05 197
2012-01-06 146
2012-01-07 98
2012-01-08 95
2012-01-09 244
2012-01-10 397
2012-01-11 273
2012-01-12 157
2012-01-13 75
2012-01-14 32
2012-01-15 54
...
2012-10-22 3650
2012-10-23 4177
2012-10-24 3744
2012-10-25 3735
2012-10-26 4290
2012-10-27 1857
2012-10-28 1310
2012-10-29 2919
2012-10-30 2887
2012-10-31 2634
2012-11-01 2405
2012-11-02 1582
2012-11-03 844
2012-11-04 966
2012-11-05 2247
Name: Berri 1, Length: 310, dtype: int64
</code></pre>

<h2 id="1-3-plotting-a-column">1.3 Plotting a column</h2>

<p>Just add <code>.plot()</code> to the end! How could it be easier? =)</p>

<p>We can see that, unsurprisingly, not many people are biking in January, February, and March.</p>

<pre><code class="language-python">import pandas as pd
import matplotlib.pyplot as plt

fixed_df = pd.read_csv('bikes.csv', sep=';', encoding='latin1', parse_dates=['Date'], dayfirst=True, index_col='Date')
fixed_df['Berri 1'].plot()
plt.show()
</code></pre>

<p>Output:
<div>
<img src="/img/plot_single_column.png" alt="Plotting CSV column with Matplotlib" />
</div>
We can also plot all the columns just as easily. We&rsquo;ll make it a little bigger, too. You can see that it&rsquo;s more squished together, but all the bike paths behave basically the same &ndash; if it&rsquo;s a bad day for cyclists, it&rsquo;s a bad day everywhere.</p>

<pre><code class="language-python">fixed_df.plot(figsize=(15, 10))
plt.show()
</code></pre>

<p>Output:</p>

<div>
<img src="/img/plot_dataframe.png" alt="Plotting Dataframe with Matplotlib" />
</div>

<h2 id="1-4-putting-all-that-together">1.4 Putting all that together</h2>

<p>Here&rsquo;s the code we needed to write do draw that graph, all together:</p>

<pre><code class="language-python">df = pd.read_csv('bikes.csv', sep=';', encoding='latin1', parse_dates=['Date'], dayfirst=True, index_col='Date')
df['Berri 1'].plot()
</code></pre>

<p>Output:
<div>
<img src="/img/plot_single_column.png" alt="Plotting CSV column with Matplotlib" />
</div></p>



</div>
</section>
<div style="font-size: medium; font-style: italic; text-align: right;">
<span style="font-size: medium; font-style: italic; text-align: left;">Last revision: May 10, 2017</span>
<span style="font-size: medium; font-style: italic; text-align: left;">Last revision: May 11, 2017</span>
</div>

</div>
Expand Down
4 changes: 2 additions & 2 deletions sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

<url>
<loc>http://tutswiki.com/</loc>
<lastmod>2017-05-10T00:00:00+00:00</lastmod>
<lastmod>2017-05-11T00:00:00+00:00</lastmod>
</url>

<url>
<loc>http://tutswiki.com/pandas-cookbook/chapter1</loc>
<lastmod>2017-05-10T00:00:00+00:00</lastmod>
<lastmod>2017-05-11T00:00:00+00:00</lastmod>
</url>

</urlset>
Loading

0 comments on commit fe1cf56

Please sign in to comment.