Problem Formulation and Solution Overview
As a Python Coder, situations arise where you will need to apply a function against elements of a Series.
Rivers Clothing does business with six (6) different countries. The Tax Rates for the associated countries have increased by 2%.
π¬ Question: How would we update the Series entries to increase the elements accordingly?
We can accomplish this task by one of the following options:
- Method 1: Use
apply()
and alambda
- Method 2: Use List Comprehension
- Method 3: Use a for loop and
iat[]
- Method 4: Use
update()
Preparation
- The Pandas library enables access to/from a DataFrame.
To install this library, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
If the installation was successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required library.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd
Method 1: Use Apply and a Lambda
You can apply a function to each element of an array by using apply()
where you pass an anonymous lambda
function. This function executes on each array element.
Here’s an example:
t_rates = [15, 17, 18, 19, 20, 21] t_series = pd.Series(t_rates, index=['CA', 'US', 'UK', 'AU', 'DK', 'DE']) t_series = t_series.apply(lambda x: x*.02+x) print(t_series)
Above is a list of current Tax Rates (t_rates
) for the six (6) countries. To update the Tax Rate, we create a Series (t_series
) from the above list by calling the pd.Series()
function and passing it two (2) parameters:
t_rates
.index
(as a List of 2-character Country Codes).
Using apply()
and a lambda
we increase each Tax Rate by 2% in a single one-liner statement.
The results save back to t_series
. As simple as that!
Output
CA | 15.30 |
US | 17.34 |
UK | 18.36 |
AU | 19.38 |
DK | 20.40 |
DE | 21.42 |
dtype: | float64 |
π‘ Note: Initially, all Tax Rates were integers. However, once the increase is applied, they automatically convert to float64
.
Method 2: Use List Comprehension
List Comprehension uses a single expression to iterate through all list elements. You can apply a particular function to each array element in the expression.
For example, you can use list comprehension to make the appropriate Tax Rate adjustments.
t_rates = [15, 17, 18, 19, 20, 21] t_series = pd.Series(t_rates, index=['CA', 'US', 'UK', 'AU', 'DK', 'DE']) t_series = [x*.02+x for(x) in t_series] print(t_series)
Above is a list of current Tax Rates (t_rates
) for the six (6) countries. To update the Tax Rate, we create a Series (t_series
) from the above list by calling the pd.Series()
function and passing it two (2) parameters:
t_rates
.index
(as a List of 2-character Country Codes).
Using List Comprehension and a for
loop, we increase each Tax Rate by 2% in a single statement. The results save back to t_series
.
Output
Unlike Method 1, the output displays on one line.
[15.3, 17.34, 18.36, 19.38, 20.4, 21.42] |
Method 3: Use a for loop and iat
Another way to handle the Tax Rate change is to use a for
loop and reference the elements using iat[]
.
The code below accesses each element via the position and updates the Tax Rate by 2% accordingly. The results save back to t_series
.
t_rates = [15, 17, 18, 19, 20, 21] t_series = pd.Series(t_rates, index=['CA', 'US', 'UK', 'AU', 'DK', 'DE']) icount = 0 for i in t_series: t_series.iat[icount] += t_series.iat[icount]*.02 icount += 1
This code is not as efficient as other methods, but the output remains the same.
Output
CA | 15.30 |
US | 17.34 |
UK | 18.36 |
AU | 19.38 |
DK | 20.40 |
DE | 21.42 |
dtype: | float64 |
π‘ Note: iat[]
is similar to iloc[]
. However, iat[]
returns a single value and therefore executes faster.
Method 4: Use update
The Series update()
method modifies the Series data in place. We recommend using this method when only a few elements require adjustments. However, the entire Series could be changed using this method.
Let’s assume two (2) countries decide to increase their Tax Rate by 2%, and the remaining Countries ignore any Tax Rate change.
t_rates = [15, 17, 18, 19, 20, 21] t_series = pd.Series(t_rates, index=['CA', 'US', 'UK', 'AU', 'DK', 'DE']) t_series.update(pd.Series([18.36, 19.38], index=['UK', 'AU'])) print(t_series)
In this code, we pass t_series.update()
the pd.Series()
a function containing two (2) parameters:
- A list of modified Tax Rates (calculated manually).
- An
index
containing a list of 2-character Country Codes. This list indicates which Country will have the new Tax Rate applied.
After running this code, the two (2) countries with the 2% tax increase applied (UK & AU) are updated. The results save back to t_series
.
Output
CA | 15.00 |
US | 17.00 |
UK | 18.18 |
AU | 19.19 |
DK | 20.00 |
DE | 21.00 |
dtype: | float64 |
π‘ Note: Initially, all Tax Rates were integers. However, once the increase is applied, they automatically convert to float64.
Summary
As you can see, there are a few ways to accomplish the same task. It is up to you to decide which method best meets your coding requirements.
Good Luck & Happy Coding!