Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Pandas apply() — A Helpful Illustrated Guide

#1
Pandas apply() — A Helpful Illustrated Guide

<div><figure class="wp-block-image size-large"><img loading="lazy" width="624" height="289" src="https://blog.finxter.com/wp-content/uploads/2020/12/image-13.png" alt="" class="wp-image-18236" srcset="https://blog.finxter.com/wp-content/uploads/2020/12/image-13.png 624w, https://blog.finxter.com/wp-content/uplo...00x139.png 300w, https://blog.finxter.com/wp-content/uplo...150x69.png 150w" sizes="(max-width: 624px) 100vw, 624px" /></figure>
<p>The Pandas <code>apply( )</code> function is used to apply the functions on the <a href="https://blog.finxter.com/pandas-quickstart/" target="_blank" rel="noreferrer noopener" title="10 Minutes to Pandas (in 5 Minutes)">Pandas </a>objects. We have so many built-in aggregation functions in pandas on Series and DataFrame objects. But, to apply some application-specific functions, we can leverage the <code>apply( )</code> function. Pandas <code>apply( )</code> is both the Series method and DataFrame method.</p>
<h1>Pandas apply function to one column – apply( ) as Series method</h1>
<p>Let’s construct a DataFrame in which we have the information of 4 persons.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" 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(
... {
... 'Name': ['Edward', 'Natalie', 'Chris M', 'Priyatham'],
... 'Sex' : ['M', 'F', 'M', 'M'],
... 'Age': [45, 35, 29, 26],
... 'weight(kgs)': [68.4, 58.2, 64.3, 53.1]
... }
... ) >>> print(df) Name Sex Age weight(kgs)
0 Edward M 45 68.4
1 Natalie F 35 58.2
2 Chris M M 29 64.3
3 Priyatham M 26 53.1</pre>
<p><code>pandas.Series.apply</code> takes any of the below two different kinds of functions as an argument.  They are:</p>
<ul>
<li>Python functions</li>
<li>Numpy’s universal functions (ufuncs)</li>
</ul>
<h2>1. Python functions</h2>
<p>In Python, there are 3 different kinds of functions in general;</p>
<ul>
<li>Built-in functions</li>
<li>User-defined functions</li>
<li>Lambda functions</li>
</ul>
<h2>a) Applying Python built-in functions on Series</h2>
<p>If we would like to know the length of the names of each person, we can do so using the <code>len( )</code> function in python.</p>
<p>For example, if we want to know the length of the “Python” string, we can get by the following code;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> len("Python")
6</pre>
<p>A single column in the DataFrame is a Series object. Now, we would like to apply the same <code>len( )</code> function on the whole “Name” column of the DataFrame.  This can be achieved using the <code>apply( )</code> function in the below code;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> df['Name'].apply(len)
0 6
1 7
2 7
3 9
Name: Name, dtype: int64</pre>
<p>If you observe the above code snippet, the <code>len</code> inside the <code>apply( )</code> function is not taking any argument. In general, any function takes some data to operate on them. In the <code>len(“Python”)</code> code snippet, it’s taking the <code>“Python”</code> string as input data to calculate its length. Here, the input data is directly taken from the Series object that called the function using <code>apply( )</code>.</p>
<p>When applying the Python functions, each value in the Series is applied one by one and returns the Series object.</p>
<p>The above process can be visualised as:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img src="https://media.giphy.com/media/LwwQ79YQfRgE7XVUuL/source.gif" alt=""/></figure>
</div>
<p>In the above visualisation, you can observe that each element of Series is applied to the function one by one.</p>
<h2>b) Applying user-defined functions on Series</h2>
<p>Let’s assume that the data we have is a year old. So, we would like to update the age of each person by adding 1. We can do so by applying a user-defined function on the Series object using the <code>apply( )</code> method.</p>
<p>The code for it is,</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> def add_age(age):
... return age + 1 >>> df['Age'].apply(add_age)
0 46
1 36
2 30
3 27
Name: Age, dtype: int64 >>> df['Age'] = df['Age'].apply(add_age) >>> df Name Sex Age weight(kgs)
0 Edward M 46 68.4
1 Natalie F 36 58.2
2 Chris M M 30 64.3
3 Priyatham M 27 53.1</pre>
<p>From the above result, the major point to be noted is,</p>
<ul>
<li>The index of the resultant <a href="https://blog.finxter.com/pandas-quickstart/" target="_blank" rel="noreferrer noopener" title="10 Minutes to Pandas (in 5 Minutes)">Series </a>is equal to the index of the caller Series object. This makes the process of adding the resultant Series as a column to the DataFrame easier.</li>
</ul>
<p>It operates in the same way as applying built-in functions. Each element in the Series is passed one by one to the function.</p>
<ul>
<li><strong>&nbsp;</strong><strong>User-defined functions are used majorly when we would like to apply some application-specific complex functions.</strong></li>
</ul>
<h2>c) Applying Lambda functions on Series</h2>
<p>Lambda functions are used a lot along with the <code>apply( ) </code>method. We used a user-defined function for an easy addition operation in the above section. Let’s achieve the same result using a Lambda function.</p>
<p>The code for it is,</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> df['Age'].apply(lambda x: x+1)
0 46
1 36
2 30
3 27
Name: Age, dtype: int64 >>> # Comparing the results of applying both the user-defined function and Lambda function
>>> df['Age'].apply(lambda x: x+1) == df['Age'].apply(add_age)
0 True
1 True
2 True
3 True
Name: Age, dtype: bool</pre>
<p>From the above result, you can observe the results of applying the user-defined function and Lambda function are the same.</p>
<ul>
<li><strong><a href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank" rel="noreferrer noopener" title="Lambda Functions in Python: A Simple Introduction">Lambda functions</a> are used majorly when we would like to apply some application-specific small functions.</strong></li>
</ul>
<h2>2. Numpy’s universal functions (ufuncs)</h2>
<p><a href="https://blog.finxter.com/numpy-tutorial/" target="_blank" rel="noreferrer noopener" title="NumPy Tutorial – Everything You Need to Know to Get Started">Numpy </a>has so many built-in universal functions (<a href="https://numpy.org/doc/stable/reference/ufuncs.html">ufuncs</a>). We can provide any of the ufuncs as an argument to the <code>apply( )</code> method on Series. A series object can be thought of as a NumPy array.</p>
<p>The difference between applying Python functions and ufuncs is;</p>
<ul>
<li>When applying the Python Functions, each element in the Series is operated one by one.</li>
<li>When applying the ufuncs, the entire Series is operated at once.</li>
</ul>
<p>Let’s choose to use a ufunc to floor the floating-point values of the weight column. We have <code>numpy.floor( )</code> ufunc to achieve this.</p>
<p>The code for it is,</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import numpy as np >>> df['weight(kgs)']
0 68.4
1 58.2
2 64.3
3 53.1
Name: weight(kgs), dtype: float64 >>> df['weight(kgs)'].apply(np.floor)
0 68.0
1 58.0
2 64.0
3 53.0
Name: weight(kgs), dtype: float64</pre>
<p>In the above result, you can observe the floored to the nearest lower decimal point value and maintain its float64 data type.</p>
<p>We can visualise the above process as:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img src="https://media.giphy.com/media/Glhk7adwbr0gRkvJdR/source.gif" alt=""/></figure>
</div>
<p>In the above visualisation, you can observe that all elements of Series are applied to the function at once.</p>
<ul>
<li><strong>Whenever we have a <code>ufunc</code> to achieve our functionality, we can use it instead of defining a Python function.</strong></li>
</ul>
<h1>Pandas apply( ) as a DataFrame method</h1>
<p>We will take a look at the official documentation of the <code>apply( )</code> method on DataFrame:</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="624" height="155" src="https://blog.finxter.com/wp-content/uploads/2020/12/image-16.png" alt="" class="wp-image-18240" srcset="https://blog.finxter.com/wp-content/uploads/2020/12/image-16.png 624w, https://blog.finxter.com/wp-content/uplo...300x75.png 300w, https://blog.finxter.com/wp-content/uplo...150x37.png 150w" sizes="(max-width: 624px) 100vw, 624px" /></figure>
<p><code>pandas.DataFrame.apply</code> has two important arguments;</p>
<ul>
<li><code>func</code> – Function to be applied along the mentioned axis</li>
<li><code>axis</code> – Axis along which function is applied</li>
</ul>
<p>Again the axis also has 2 possible values;</p>
<ol type="1">
<li><code>axis=0</code> – Apply function to multiple columns</li>
<li><code>axis=1</code> – Apply function to every row</li>
</ol>
<h2>1. Pandas apply function to multiple columns</h2>
<p>Let’s say the people in our dataset provided their height (in cms) information. It can be added using the following code,</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> df['height(cms)'] = [178, 160, 173, 168]
>>> df Name Sex Age weight(kgs) height(cms)
0 Edward M 45 68.4 178
1 Natalie F 35 58.2 160
2 Chris M M 29 64.3 173
3 Priyatham M 26 53.1 168</pre>
<p>We’ll make the “Name” column the index of the DataFrame. Also, we’ll get the subset of the DataFrame with “Age”, “weight(kgs)”, and “height(cms)” columns.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> data = df.set_index('Name')
>>> data Sex Age weight(kgs) height(cms)
Name Edward M 45 68.4 178
Natalie F 35 58.2 160
Chris M M 29 64.3 173
Priyatham M 26 53.1 168 >>> data_subset = data[['Age', 'weight(kgs)', 'height(cms)']]
>>> data_subset Age weight(kgs) height(cms)
Name Edward 45 68.4 178
Natalie 35 58.2 160
Chris M 29 64.3 173
Priyatham 26 53.1 168</pre>
<p>If we would like to get the average age, weight, and height of all the people, we can use the numpy <code>ufunc</code> <code><a href="https://blog.finxter.com/how-to-calculate-row-variance-numpy-array/" target="_blank" rel="noreferrer noopener" title="Python Numpy 101: How to Calculate the Row Variance of a Numpy 2D Array?">numpy.mean( )</a></code>.</p>
<p>The code for it is,</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import numpy as np
>>> data_subset.apply(np.mean, axis=0)
Age 33.75
weight(kgs) 61.00
height(cms) 169.75
dtype: float64</pre>
<p>We directly have a Pandas DataFrame aggregation function called <code>mean( )</code> which does the same as above;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> data_subset.mean()
Age 33.75
weight(kgs) 61.00
height(cms) 169.75
dtype: float64</pre>
<p>If you observe the results above, the results of Pandas DataFrame aggregation function and applying <code>ufunc</code> are equal. So, we don’t use the <code>apply( )</code> method in such simple scenarios where we have aggregation functions available.</p>
<ul>
<li><strong>Whenever you have to apply some complex functions on DataFrames, then use the <code>apply( )</code> method.</strong></li>
</ul>
<h2>2. Pandas apply function to every row</h2>
<p>Based upon the height and weight, we can know whether they’re fit or thin, or obese. The fitness criteria are different for men and women as setup by international standards. Let’s grab the fitness criteria data for the heights and weights of the people in our data.</p>
<p>This can be represented using a dictionary;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> male_fitness = {
... #height : (weight_lower_cap, weight_upper_cap)
... 178 : ( 67.5 , 83 ),
... 173 : ( 63 , 70.6 ),
... 168 : ( 58 , 70.7 )
... }
>>> female_fitness = {
... #height : (weight_lower_cap, weight_upper_cap)
... 160 : ( 47.2 , 57.6 )
... }</pre>
<p>In the above dictionary, the keys are the heights and the values are tuples of the lower and upper limit of ideal weight respectively.</p>
<p>If someone is below the ideal weight for their respective height, they are “Thin”. If someone is above the ideal weight for their respective height, they are “Obese”. If someone is in the range of ideal weight for their respective height, they are “Fit”.</p>
<p>Let’s build a function that can be used in the <code>apply( )</code> method that takes all the rows one by one.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> def fitness_check(seq):
... if seq.loc['Sex'] == 'M':
... if (seq.loc['weight(kgs)'] > male_fitness[seq.loc['height(cms)']][0]) &amp; (seq.loc['weight(kgs)'] &lt; male_fitness[seq.loc['height(cms)']][1]):
... return "Fit"
... elif (seq.loc['weight(kgs)'] &lt; male_fitness[seq.loc['height(cms)']][0]):
... return "Thin"
... else:
... return "Obese"
... else:
... if (seq.loc['weight(kgs)'] > female_fitness[seq.loc['height(cms)']][0]) &amp; (seq.loc['weight(kgs)'] &lt; female_fitness[seq.loc['height(cms)']][1]):
... return "Fit"
... elif (seq.loc['weight(kgs)'] &lt; female_fitness[seq.loc['height(cms)']][0]):
... return "Thin"
... else:
... return "Obese"</pre>
<p>The function returns whether a given person is “Fit” or “Thin” or “Obese”. It uses the different fitness criteria dictionaries for male and female created above.</p>
<p>Finally, let’s apply the above function to every row using the <code>apply( )</code> method;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> data.apply(fitness_check, axis=1)
Name
Edward Fit
Natalie Obese
Chris M Fit
Priyatham Thin
dtype: object</pre>
<p>From the above result, we got to know who is Fit or Thin or Obese.</p>
<h1>Conclusion and Next Steps</h1>
<p>Using the <code>apply( )</code> method when you want to achieve some complex functionality is preferred and recommended. Mostly built-in aggregation functions in Pandas come in handy. If you liked this tutorial on the <code>apply( )</code> function and like quiz-based learning, please consider giving it a try to read our <a href="https://www.amazon.com/gp/product/B08NG8QHW7/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B08NG8QHW7&amp;linkCode=as2&amp;tag=finxter-20&amp;linkId=05c3c3b09840ec9cc56d5a7cad5a6398" target="_blank" rel="noreferrer noopener">Coffee Break Pandas</a> book.</p>
</p>
<p>The post <a href="https://blog.finxter.com/pandas-apply-a-helpful-illustrated-guide/" target="_blank" rel="noopener noreferrer">Pandas apply() — A Helpful Illustrated Guide</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/12/...ted-guide/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016