Use apply() function for a single column in Pandas

Pandas is a popular Python library used for data analysis and data manipulation. It is an open-source, fast, easy-to-use, powerful library built on top of the NumPy library, which enables users to work with multi-dimensional arrays and matrices. In this tutorial, let’s learn how to use the apply() function in Pandas for a single column in the data frame. The apply() function allows us to apply a function along an axis in the dataframe.

For this tutorial, I am using a google colab notebook. It has the Pandas library preinstalled in it. But if you are using your local environment, first make sure your system has Python installed and run the below command.

pip install pandas

Pandas requires NumPy to work, but pip will automatically install it when you install pandas.

Using the apply() function on a single column

Import the Pandas library into the file you are working on. Here pd is an alias name for Pandas.

import pandas as pd

Create a dictionary in Python called fruits with 2 columns called item and prices. Fill these columns with the data shown below.

fruits = {
    "item":["orange", "apple", "kiwi", "bannana", "litchi"],
    "price":[50, 120, 40, 20, 35]
}

Now convert this Python dictionary to a Pandas dataframe to use Pandas functions on it using the below code.

df = pd.DataFrame(fruits)

Print the dataframe using the below code.

print(df)

Output:

  item   price 
0 orange  50 
1 apple   120 
2 kiwi    40 
3 bannan  20 
4 litchi  35

Let’s say we want to increase all fruit prices by 20 rupees in the prices column. We can use the below code to just modify the prices column using the apply() function. Here we are using the lambda function inside apply() function to modify the column by iterating and adding 20 to each price value.

df['price'] = df['price'].apply(lambda i:i+20)
print(df)

Output:

  item   price 
0 orange  70 
1 apple   140 
2 kiwi    60 
3 bannana 40 
4 litchi  55

If you don’t want to use a lambda function but instead a regular Python function, then follow the below code. Here we are taking each row value in the price column and adding 20 to it.

def add_price(x):
  return x + 20
df['price'] = df['price'].apply(add_price)
print(df)

Output:

  item   price 
0 orange  70 
1 apple   140 
2 kiwi    60 
3 bannana 40 
4 litchi  55

Leave a Reply

Your email address will not be published. Required fields are marked *