Skip to content

Commit b6d858c

Browse files
add applymap
1 parent eb9e045 commit b6d858c

File tree

9 files changed

+190
-102
lines changed

9 files changed

+190
-102
lines changed

Chapter3/change_values.ipynb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,65 @@
323323
"df"
324324
]
325325
},
326+
{
327+
"cell_type": "markdown",
328+
"id": "cd0b4dc5",
329+
"metadata": {},
330+
"source": [
331+
"### Apply a Function to a DataFrame Elementwise"
332+
]
333+
},
334+
{
335+
"cell_type": "markdown",
336+
"id": "af6b63fb",
337+
"metadata": {},
338+
"source": [
339+
"The `apply` method applies a function along an axis of a DataFrame. If you want to apply a function to a DataFrame elementwise, use `applymap`."
340+
]
341+
},
342+
{
343+
"cell_type": "code",
344+
"execution_count": 8,
345+
"id": "82e5286d",
346+
"metadata": {},
347+
"outputs": [
348+
{
349+
"name": "stdout",
350+
"output_type": "stream",
351+
"text": [
352+
" col1 col2\n",
353+
"0 2 3\n",
354+
"1 9 7\n"
355+
]
356+
}
357+
],
358+
"source": [
359+
"import pandas as pd \n",
360+
"\n",
361+
"df = pd.DataFrame({\"col1\": [2, 9], \"col2\": [3, 7]})\n",
362+
"print(df )"
363+
]
364+
},
365+
{
366+
"cell_type": "code",
367+
"execution_count": 9,
368+
"id": "f71e2405",
369+
"metadata": {},
370+
"outputs": [
371+
{
372+
"name": "stdout",
373+
"output_type": "stream",
374+
"text": [
375+
" col1 col2\n",
376+
"0 failed failed\n",
377+
"1 passed passed\n"
378+
]
379+
}
380+
],
381+
"source": [
382+
"print(df.applymap(lambda val: 'failed' if val < 5 else 'passed'))"
383+
]
384+
},
326385
{
327386
"cell_type": "markdown",
328387
"id": "9c4ec079",

Chapter3/style_dataframe.ipynb

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,7 @@
5656
"outputs": [
5757
{
5858
"data": {
59-
"application/javascript": [
60-
"\n",
61-
" setTimeout(function() {\n",
62-
" var nbb_cell_id = 33;\n",
63-
" var nbb_unformatted_code = \"def highlight_number(row):\\n return [\\n 'background-color: red; color: white' if cell <= 0\\n else 'background-color: green; color: white'\\n for cell in row\\n ]\";\n",
64-
" var nbb_formatted_code = \"def highlight_number(row):\\n return [\\n \\\"background-color: red; color: white\\\"\\n if cell <= 0\\n else \\\"background-color: green; color: white\\\"\\n for cell in row\\n ]\";\n",
65-
" var nbb_cells = Jupyter.notebook.get_cells();\n",
66-
" for (var i = 0; i < nbb_cells.length; ++i) {\n",
67-
" if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n",
68-
" if (nbb_cells[i].get_text() == nbb_unformatted_code) {\n",
69-
" nbb_cells[i].set_text(nbb_formatted_code);\n",
70-
" }\n",
71-
" break;\n",
72-
" }\n",
73-
" }\n",
74-
" }, 500);\n",
75-
" "
76-
],
59+
"application/javascript": "\n setTimeout(function() {\n var nbb_cell_id = 33;\n var nbb_unformatted_code = \"def highlight_number(row):\\n return [\\n 'background-color: red; color: white' if cell <= 0\\n else 'background-color: green; color: white'\\n for cell in row\\n ]\";\n var nbb_formatted_code = \"def highlight_number(row):\\n return [\\n \\\"background-color: red; color: white\\\"\\n if cell <= 0\\n else \\\"background-color: green; color: white\\\"\\n for cell in row\\n ]\";\n var nbb_cells = Jupyter.notebook.get_cells();\n for (var i = 0; i < nbb_cells.length; ++i) {\n if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n if (nbb_cells[i].get_text() == nbb_unformatted_code) {\n nbb_cells[i].set_text(nbb_formatted_code);\n }\n break;\n }\n }\n }, 500);\n ",
7760
"text/plain": [
7861
"<IPython.core.display.Javascript object>"
7962
]
@@ -94,7 +77,7 @@
9477
},
9578
{
9679
"cell_type": "code",
97-
"execution_count": 34,
80+
"execution_count": null,
9881
"id": "6dbee122",
9982
"metadata": {
10083
"ExecuteTime": {
@@ -152,30 +135,12 @@
152135
"<pandas.io.formats.style.Styler at 0x7f5eb8a64460>"
153136
]
154137
},
155-
"execution_count": 34,
156138
"metadata": {},
157-
"output_type": "execute_result"
139+
"output_type": "display_data"
158140
},
159141
{
160142
"data": {
161-
"application/javascript": [
162-
"\n",
163-
" setTimeout(function() {\n",
164-
" var nbb_cell_id = 34;\n",
165-
" var nbb_unformatted_code = \"df.style.apply(highlight_number)\";\n",
166-
" var nbb_formatted_code = \"df.style.apply(highlight_number)\";\n",
167-
" var nbb_cells = Jupyter.notebook.get_cells();\n",
168-
" for (var i = 0; i < nbb_cells.length; ++i) {\n",
169-
" if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n",
170-
" if (nbb_cells[i].get_text() == nbb_unformatted_code) {\n",
171-
" nbb_cells[i].set_text(nbb_formatted_code);\n",
172-
" }\n",
173-
" break;\n",
174-
" }\n",
175-
" }\n",
176-
" }, 500);\n",
177-
" "
178-
],
143+
"application/javascript": "\n setTimeout(function() {\n var nbb_cell_id = 34;\n var nbb_unformatted_code = \"df.style.apply(highlight_number)\";\n var nbb_formatted_code = \"df.style.apply(highlight_number)\";\n var nbb_cells = Jupyter.notebook.get_cells();\n for (var i = 0; i < nbb_cells.length; ++i) {\n if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n if (nbb_cells[i].get_text() == nbb_unformatted_code) {\n nbb_cells[i].set_text(nbb_formatted_code);\n }\n break;\n }\n }\n }, 500);\n ",
179144
"text/plain": [
180145
"<IPython.core.display.Javascript object>"
181146
]

Chapter5/visualization.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,7 @@
23852385
},
23862386
{
23872387
"cell_type": "markdown",
2388-
"id": "64d4c861",
2388+
"id": "b0ad6f37",
23892389
"metadata": {},
23902390
"source": [
23912391
"### Lux: A Python API for Intelligent Visual Discovery"
@@ -2411,14 +2411,14 @@
24112411
},
24122412
{
24132413
"cell_type": "markdown",
2414-
"id": "520b2b71",
2414+
"id": "39cc1243",
24152415
"metadata": {},
24162416
"source": [
24172417
"Have you ever taken a while to come up with the visualizations for your data? Wouldn't it be nice if you can get some suggestions on what to visualize?\n",
24182418
"\n",
24192419
"That is when Lux comes in handy. Lux is a Python library that makes it fast and easy to explore data. \n",
24202420
"\n",
2421-
"After lux is imported, you will get a set of visualizations when printing your DataFrame."
2421+
"After Lux is imported, you will get a set of visualizations when printing your DataFrame."
24222422
]
24232423
},
24242424
{
@@ -2442,7 +2442,7 @@
24422442
{
24432443
"cell_type": "code",
24442444
"execution_count": 22,
2445-
"id": "b45172f0",
2445+
"id": "84e10460",
24462446
"metadata": {
24472447
"ExecuteTime": {
24482448
"end_time": "2022-04-18T13:34:55.540509Z",
@@ -2545,15 +2545,15 @@
25452545
},
25462546
{
25472547
"cell_type": "markdown",
2548-
"id": "fa03dd13",
2548+
"id": "ffb454c1",
25492549
"metadata": {},
25502550
"source": [
25512551
"![](../img/lux.gif)"
25522552
]
25532553
},
25542554
{
25552555
"cell_type": "markdown",
2556-
"id": "d231ea50",
2556+
"id": "d7252f38",
25572557
"metadata": {},
25582558
"source": [
25592559
"[Link to Lux](https://github.com/lux-org/lux)."

docs/Chapter3/change_values.html

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -652,29 +652,34 @@ <h1 class="site-logo" id="site-title">Effective Python for Data Scientists</h1>
652652
4.1.2. Apply a Function to a Column of a DataFrame
653653
</a>
654654
</li>
655+
<li class="toc-h2 nav-item toc-entry">
656+
<a class="reference internal nav-link" href="#apply-a-function-to-a-dataframe-elementwise">
657+
4.1.3. Apply a Function to a DataFrame Elementwise
658+
</a>
659+
</li>
655660
<li class="toc-h2 nav-item toc-entry">
656661
<a class="reference internal nav-link" href="#assign-values-to-multiple-new-columns">
657-
4.1.3. Assign Values to Multiple New Columns
662+
4.1.4. Assign Values to Multiple New Columns
658663
</a>
659664
</li>
660665
<li class="toc-h2 nav-item toc-entry">
661666
<a class="reference internal nav-link" href="#pandas-series-map-change-values-of-a-pandas-series-using-a-dictionary">
662-
4.1.4. pandas.Series.map: Change Values of a Pandas Series Using a Dictionary
667+
4.1.5. pandas.Series.map: Change Values of a Pandas Series Using a Dictionary
663668
</a>
664669
</li>
665670
<li class="toc-h2 nav-item toc-entry">
666671
<a class="reference internal nav-link" href="#pandas-dataframe-explode-transform-each-element-in-an-iterable-to-a-row">
667-
4.1.5. pandas.DataFrame.explode: Transform Each Element in an Iterable to a Row
672+
4.1.6. pandas.DataFrame.explode: Transform Each Element in an Iterable to a Row
668673
</a>
669674
</li>
670675
<li class="toc-h2 nav-item toc-entry">
671676
<a class="reference internal nav-link" href="#split-a-string-into-multiple-rows">
672-
4.1.6. Split a String into Multiple Rows
677+
4.1.7. Split a String into Multiple Rows
673678
</a>
674679
</li>
675680
<li class="toc-h2 nav-item toc-entry">
676681
<a class="reference internal nav-link" href="#forward-fill-in-pandas-use-the-previous-value-to-fill-the-current-missing-value">
677-
4.1.7. Forward Fill in pandas: Use the Previous Value to Fill the Current Missing Value
682+
4.1.8. Forward Fill in pandas: Use the Previous Value to Fill the Current Missing Value
678683
</a>
679684
</li>
680685
</ul>
@@ -891,8 +896,43 @@ <h2><span class="section-number">4.1.2. </span>Apply a Function to a Column of a
891896
</script></div>
892897
</div>
893898
</div>
899+
<div class="section" id="apply-a-function-to-a-dataframe-elementwise">
900+
<h2><span class="section-number">4.1.3. </span>Apply a Function to a DataFrame Elementwise<a class="headerlink" href="#apply-a-function-to-a-dataframe-elementwise" title="Permalink to this headline"></a></h2>
901+
<p>The <code class="docutils literal notranslate"><span class="pre">apply</span></code> method applies a function along an axis of a DataFrame. If you want to apply a function to a DataFrame elementwise, use <code class="docutils literal notranslate"><span class="pre">applymap</span></code>.</p>
902+
<div class="cell docutils container">
903+
<div class="cell_input docutils container">
904+
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
905+
906+
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s2">&quot;col1&quot;</span><span class="p">:</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span class="s2">&quot;col2&quot;</span><span class="p">:</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">]})</span>
907+
<span class="nb">print</span><span class="p">(</span><span class="n">df</span> <span class="p">)</span>
908+
</pre></div>
909+
</div>
910+
</div>
911+
<div class="cell_output docutils container">
912+
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> col1 col2
913+
0 2 3
914+
1 9 7
915+
</pre></div>
916+
</div>
917+
</div>
918+
</div>
919+
<div class="cell docutils container">
920+
<div class="cell_input docutils container">
921+
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">applymap</span><span class="p">(</span><span class="k">lambda</span> <span class="n">val</span><span class="p">:</span> <span class="s1">&#39;failed&#39;</span> <span class="k">if</span> <span class="n">val</span> <span class="o">&lt;</span> <span class="mi">5</span> <span class="k">else</span> <span class="s1">&#39;passed&#39;</span><span class="p">))</span>
922+
</pre></div>
923+
</div>
924+
</div>
925+
<div class="cell_output docutils container">
926+
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> col1 col2
927+
0 failed failed
928+
1 passed passed
929+
</pre></div>
930+
</div>
931+
</div>
932+
</div>
933+
</div>
894934
<div class="section" id="assign-values-to-multiple-new-columns">
895-
<h2><span class="section-number">4.1.3. </span>Assign Values to Multiple New Columns<a class="headerlink" href="#assign-values-to-multiple-new-columns" title="Permalink to this headline"></a></h2>
935+
<h2><span class="section-number">4.1.4. </span>Assign Values to Multiple New Columns<a class="headerlink" href="#assign-values-to-multiple-new-columns" title="Permalink to this headline"></a></h2>
896936
<p>If you want to assign values to multiple new columns, instead of assigning them separately, you can do everything in one line of code with <code class="docutils literal notranslate"><span class="pre">df.assign</span></code>.</p>
897937
<p>In the code below, I first created <code class="docutils literal notranslate"><span class="pre">col3</span></code> then use <code class="docutils literal notranslate"><span class="pre">col3</span></code> to create <code class="docutils literal notranslate"><span class="pre">col4</span></code>. Everything is in one line of code.</p>
898938
<div class="cell docutils container">
@@ -954,7 +994,7 @@ <h2><span class="section-number">4.1.3. </span>Assign Values to Multiple New Col
954994
</div>
955995
</div>
956996
<div class="section" id="pandas-series-map-change-values-of-a-pandas-series-using-a-dictionary">
957-
<h2><span class="section-number">4.1.4. </span>pandas.Series.map: Change Values of a Pandas Series Using a Dictionary<a class="headerlink" href="#pandas-series-map-change-values-of-a-pandas-series-using-a-dictionary" title="Permalink to this headline"></a></h2>
997+
<h2><span class="section-number">4.1.5. </span>pandas.Series.map: Change Values of a Pandas Series Using a Dictionary<a class="headerlink" href="#pandas-series-map-change-values-of-a-pandas-series-using-a-dictionary" title="Permalink to this headline"></a></h2>
958998
<p>If you want to change values of a pandas Series using a dictionary, use <code class="docutils literal notranslate"><span class="pre">pd.Series.map</span></code>.</p>
959999
<div class="cell docutils container">
9601000
<div class="cell_input docutils container">
@@ -996,7 +1036,7 @@ <h2><span class="section-number">4.1.4. </span>pandas.Series.map: Change Values
9961036
</div>
9971037
</div>
9981038
<div class="section" id="pandas-dataframe-explode-transform-each-element-in-an-iterable-to-a-row">
999-
<h2><span class="section-number">4.1.5. </span>pandas.DataFrame.explode: Transform Each Element in an Iterable to a Row<a class="headerlink" href="#pandas-dataframe-explode-transform-each-element-in-an-iterable-to-a-row" title="Permalink to this headline"></a></h2>
1039+
<h2><span class="section-number">4.1.6. </span>pandas.DataFrame.explode: Transform Each Element in an Iterable to a Row<a class="headerlink" href="#pandas-dataframe-explode-transform-each-element-in-an-iterable-to-a-row" title="Permalink to this headline"></a></h2>
10001040
<p>When working with <code class="docutils literal notranslate"><span class="pre">pandas</span> <span class="pre">DataFrame</span></code>, if you want to transform each element in an iterable to a row, use <code class="docutils literal notranslate"><span class="pre">explode</span></code>.</p>
10011041
<div class="cell docutils container">
10021042
<div class="cell_input docutils container">
@@ -1116,7 +1156,7 @@ <h2><span class="section-number">4.1.5. </span>pandas.DataFrame.explode: Transfo
11161156
</div>
11171157
</div>
11181158
<div class="section" id="split-a-string-into-multiple-rows">
1119-
<h2><span class="section-number">4.1.6. </span>Split a String into Multiple Rows<a class="headerlink" href="#split-a-string-into-multiple-rows" title="Permalink to this headline"></a></h2>
1159+
<h2><span class="section-number">4.1.7. </span>Split a String into Multiple Rows<a class="headerlink" href="#split-a-string-into-multiple-rows" title="Permalink to this headline"></a></h2>
11201160
<p>Sometimes, you might have a column whose values are strings representing different items such as <code class="docutils literal notranslate"><span class="pre">&quot;1,</span> <span class="pre">2&quot;</span></code>.</p>
11211161
<div class="cell docutils container">
11221162
<div class="cell_input docutils container">
@@ -1269,7 +1309,7 @@ <h2><span class="section-number">4.1.6. </span>Split a String into Multiple Rows
12691309
</div>
12701310
</div>
12711311
<div class="section" id="forward-fill-in-pandas-use-the-previous-value-to-fill-the-current-missing-value">
1272-
<h2><span class="section-number">4.1.7. </span>Forward Fill in pandas: Use the Previous Value to Fill the Current Missing Value<a class="headerlink" href="#forward-fill-in-pandas-use-the-previous-value-to-fill-the-current-missing-value" title="Permalink to this headline"></a></h2>
1312+
<h2><span class="section-number">4.1.8. </span>Forward Fill in pandas: Use the Previous Value to Fill the Current Missing Value<a class="headerlink" href="#forward-fill-in-pandas-use-the-previous-value-to-fill-the-current-missing-value" title="Permalink to this headline"></a></h2>
12731313
<p>If you want to use the previous value in a column or a row to fill the current missing value in a pandas DataFrame, use <code class="docutils literal notranslate"><span class="pre">df.fillna(method=’ffill’)</span></code>. <code class="docutils literal notranslate"><span class="pre">ffill</span></code> stands for forward fill.</p>
12741314
<div class="cell docutils container">
12751315
<div class="cell_input docutils container">

docs/Chapter5/visualization.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ <h2><span class="section-number">6.10.15. </span>Lux: A Python API for Intellige
18931893
</div>
18941894
<p>Have you ever taken a while to come up with the visualizations for your data? Wouldn’t it be nice if you can get some suggestions on what to visualize?</p>
18951895
<p>That is when Lux comes in handy. Lux is a Python library that makes it fast and easy to explore data.</p>
1896-
<p>After lux is imported, you will get a set of visualizations when printing your DataFrame.</p>
1896+
<p>After Lux is imported, you will get a set of visualizations when printing your DataFrame.</p>
18971897
<div class="cell docutils container">
18981898
<div class="cell_input docutils container">
18991899
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">load_wine</span>

docs/_sources/Chapter3/change_values.ipynb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,65 @@
323323
"df"
324324
]
325325
},
326+
{
327+
"cell_type": "markdown",
328+
"id": "cd0b4dc5",
329+
"metadata": {},
330+
"source": [
331+
"### Apply a Function to a DataFrame Elementwise"
332+
]
333+
},
334+
{
335+
"cell_type": "markdown",
336+
"id": "af6b63fb",
337+
"metadata": {},
338+
"source": [
339+
"The `apply` method applies a function along an axis of a DataFrame. If you want to apply a function to a DataFrame elementwise, use `applymap`."
340+
]
341+
},
342+
{
343+
"cell_type": "code",
344+
"execution_count": 8,
345+
"id": "82e5286d",
346+
"metadata": {},
347+
"outputs": [
348+
{
349+
"name": "stdout",
350+
"output_type": "stream",
351+
"text": [
352+
" col1 col2\n",
353+
"0 2 3\n",
354+
"1 9 7\n"
355+
]
356+
}
357+
],
358+
"source": [
359+
"import pandas as pd \n",
360+
"\n",
361+
"df = pd.DataFrame({\"col1\": [2, 9], \"col2\": [3, 7]})\n",
362+
"print(df )"
363+
]
364+
},
365+
{
366+
"cell_type": "code",
367+
"execution_count": 9,
368+
"id": "f71e2405",
369+
"metadata": {},
370+
"outputs": [
371+
{
372+
"name": "stdout",
373+
"output_type": "stream",
374+
"text": [
375+
" col1 col2\n",
376+
"0 failed failed\n",
377+
"1 passed passed\n"
378+
]
379+
}
380+
],
381+
"source": [
382+
"print(df.applymap(lambda val: 'failed' if val < 5 else 'passed'))"
383+
]
384+
},
326385
{
327386
"cell_type": "markdown",
328387
"id": "9c4ec079",

0 commit comments

Comments
 (0)