Skip to content

Commit 51674d6

Browse files
authored
Merge pull request #160 from pryn-kb/main
extra exercise in pandas 03
2 parents 5acc5d8 + 6680502 commit 51674d6

File tree

8 files changed

+669
-427
lines changed

8 files changed

+669
-427
lines changed
7.62 KB
Binary file not shown.
1.34 KB
Binary file not shown.

_build/html/_sources/docs/pandas/03_Pandas_subsets.ipynb

Lines changed: 186 additions & 112 deletions
Large diffs are not rendered by default.

_build/html/docs/pandas/03_Pandas_subsets.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ <h2> Contents </h2>
425425
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#combining-loc-and-iloc">Combining <code class="docutils literal notranslate"><span class="pre">loc</span></code> and <code class="docutils literal notranslate"><span class="pre">iloc</span></code></a></li>
426426
</ul>
427427
</li>
428+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise"><i class="fa-solid fa-pencil"></i> Exercise</a></li>
428429
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#key-points">Key points</a></li>
429430
</ul>
430431
</nav>
@@ -2287,6 +2288,40 @@ <h3>Combining <code class="docutils literal notranslate"><span class="pre">loc</
22872288
</div>
22882289
</section>
22892290
</section>
2291+
<section id="exercise">
2292+
<h2><i class="fa-solid fa-pencil"></i> Exercise<a class="headerlink" href="#exercise" title="Link to this heading">#</a></h2>
2293+
<ol class="arabic simple">
2294+
<li><p>What is the number of matching records from:</p></li>
2295+
</ol>
2296+
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">titanic</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:</span><span class="mi">100</span><span class="p">]</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">titanic</span><span class="p">[</span><span class="s1">&#39;Age&#39;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">35</span><span class="p">]</span>
2297+
</pre></div>
2298+
</div>
2299+
<ol class="arabic simple" start="2">
2300+
<li><p>What is the number of matching records from:</p></li>
2301+
</ol>
2302+
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">titanic</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">titanic</span><span class="p">[</span><span class="s1">&#39;Age&#39;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">35</span><span class="p">]</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:</span><span class="mi">100</span><span class="p">]</span>
2303+
</pre></div>
2304+
</div>
2305+
<ol class="arabic simple" start="3">
2306+
<li><p>Reflect on and explain why the two statements return a different number of rows?</p></li>
2307+
</ol>
2308+
<div class="dropdown admonition">
2309+
<p class="admonition-title">Solution</p>
2310+
<p>(We can use the <code class="docutils literal notranslate"><span class="pre">len()</span></code> function to solve question 1 and 2.)</p>
2311+
<ol class="arabic simple">
2312+
<li><p>17 rows</p></li>
2313+
<li><p>100 rows</p></li>
2314+
<li><p>The two expressions differ in the <strong>order</strong> of filtering and slicing, which affects the final result:</p></li>
2315+
</ol>
2316+
<ul class="simple">
2317+
<li><p><code class="docutils literal notranslate"><span class="pre">titanic.iloc[:100].loc[titanic['Age']</span> <span class="pre">&gt;</span> <span class="pre">35]</span></code><br />
2318+
→ First selects the <strong>first 100 rows</strong>, then filters for rows where <code class="docutils literal notranslate"><span class="pre">Age</span> <span class="pre">&gt;</span> <span class="pre">35</span></code> <strong>within those 100</strong>.</p></li>
2319+
<li><p><code class="docutils literal notranslate"><span class="pre">titanic.loc[titanic['Age']</span> <span class="pre">&gt;</span> <span class="pre">35].iloc[:100]</span></code><br />
2320+
→ First filters for <strong>all rows</strong> where <code class="docutils literal notranslate"><span class="pre">Age</span> <span class="pre">&gt;</span> <span class="pre">35</span></code>, then selects the <strong>first 100 rows</strong> from that filtered set.</p></li>
2321+
</ul>
2322+
<p>Thus, the main difference lies in <strong>whether you slice first or filter first</strong>, which can lead to <strong>very different subsets of data</strong>.</p>
2323+
</div>
2324+
</section>
22902325
<hr class="docutils" />
22912326
<section id="key-points">
22922327
<h2>Key points<a class="headerlink" href="#key-points" title="Link to this heading">#</a></h2>
@@ -2385,6 +2420,7 @@ <h2>Key points<a class="headerlink" href="#key-points" title="Link to this headi
23852420
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#combining-loc-and-iloc">Combining <code class="docutils literal notranslate"><span class="pre">loc</span></code> and <code class="docutils literal notranslate"><span class="pre">iloc</span></code></a></li>
23862421
</ul>
23872422
</li>
2423+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise"><i class="fa-solid fa-pencil"></i> Exercise</a></li>
23882424
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#key-points">Key points</a></li>
23892425
</ul>
23902426
</nav></div>

_build/html/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)