[Tut] How to Apply a Function to Each Cell in a Pandas DataFrame? - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Apply a Function to Each Cell in a Pandas DataFrame? (/thread-99844.html) |
[Tut] How to Apply a Function to Each Cell in a Pandas DataFrame? - xSicKxBot - 08-23-2022 How to Apply a Function to Each Cell in a Pandas DataFrame? <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"595293","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </div> <h2>Problem Formulation</h2> <p>Given the following DataFrame <code>df</code>:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd df = pd.DataFrame([{'A':1, 'B':2, 'C':2, 'D':4}, {'A':4, 'B':8, 'C':3, 'D':1}, {'A':2, 'B':7, 'C':1, 'D':2}, {'A':3, 'B':5, 'C':1, 'D':2}]) print(df) ''' A B C D 0 1 2 2 4 1 4 8 3 1 2 2 7 1 2 3 3 5 1 2 '''</pre> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Challenge</strong>: How to apply a function <code>f</code> to each cell in the DataFrame? </p> <p>For example, you may want to apply a function that replaces all odd values with the value <code>'odd'</code>.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="16" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd df = pd.DataFrame([{'A':1, 'B':2, 'C':2, 'D':4}, {'A':4, 'B':8, 'C':3, 'D':1}, {'A':2, 'B':7, 'C':1, 'D':2}, {'A':3, 'B':5, 'C':1, 'D':2}]) def f(cell): if cell%2 == 1: return 'odd' return cell # ... <Apply Function f to each cell> ... print(df) ''' A B C D 0 odd 2 2 4 1 4 8 odd odd 2 2 odd odd 2 3 odd odd odd 2 '''</pre> <h2>Solution: DataFrame applymap()</h2> <p class="has-global-color-8-background-color has-background">The Pandas DataFrame <code>df.applymap()</code> method returns a new DataFrame where the function <code>f</code> is applied to each cell of the original DataFrame <code>df</code>. You can pass any function object as a single argument into the <code>df.applymap()</code> function, either defined as a lambda expression or a normal function.</p> <h2>Example 1: Replace Odd Values in DataFrame</h2> <p>Here’s an example where each cell of the DataFrame is checked against whether it is an odd value. If so, it is replaced with the string <code>'odd'</code>:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def f(cell): if cell%2 == 1: return 'odd' return cell df_new = df.applymap(f) print(df_new) ''' A B C D 0 odd 2 2 4 1 4 8 odd odd 2 2 odd odd 2 3 odd odd odd 2 ''' </pre> <h2>Example 2: Create Two DataFrames with Even and Odd Values Replaced</h2> <p>A slightly advanced example uses two <a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="post" data-id="2701" target="_blank">lambda functions</a> to create two new DataFrames where one has all odd and the other has all even values replaced:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="10,11" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd df = pd.DataFrame([{'A':1, 'B':2, 'C':2, 'D':4}, {'A':4, 'B':8, 'C':3, 'D':1}, {'A':2, 'B':7, 'C':1, 'D':2}, {'A':3, 'B':5, 'C':1, 'D':2}]) df_even = df.applymap(lambda x: 'odd' if x%2 else x) df_odd = df.applymap(lambda x: x if x%2 else 'even') print(df_even) ''' A B C D 0 odd 2 2 4 1 4 8 odd odd 2 2 odd odd 2 3 odd odd odd 2 ''' print(df_odd) ''' A B C D 0 1 even even even 1 even even 3 1 2 even 7 1 even 3 3 5 1 even '''</pre> <p>We used the concept of a <strong><em>ternary operator</em></strong> to concisely define the replacement function using the keyword <code><a href="https://blog.finxter.com/python-ternary-lambda/" data-type="post" data-id="10752" target="_blank" rel="noreferrer noopener">lambda</a></code> to create a function object “on the fly”. </p> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/python-one-line-ternary/" data-type="post" data-id="10641">Understanding the Ternary Operator in Python</a></p> <p>This tutorial idea was proposed by <a href="https://blog.finxter.com/email-academy/" data-type="page" data-id="12278" target="_blank" rel="noreferrer noopener">Finxter student</a> <strong>Kyriakos</strong>. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2764.png" alt="❤" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> </div> https://www.sickgaming.net/blog/2022/08/20/how-to-apply-a-function-to-each-cell-in-a-pandas-dataframe/ |