Using Pandas apply() Function for Data Manipulation
Applying a Function to a Single Column using apply()
One of the most common use cases for the Pandas apply() function is applying a function to a single column of a DataFrame. This is often used to map values in a column to some new set of values, or to apply a transformation to the values of a column. This is a very useful technique for data cleaning and data manipulation, as it allows for fast and efficient transformations of large amounts of data.
To apply a function to a single column using apply(), we need to first select the column of interest from the DataFrame. We can do this using either the column label or the column index. Once we have selected the column, we can apply a function to it using the apply() method.
Here’s an example of how we can apply a function to a column in a Pandas DataFrame:
import pandas as pd
# create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
# define a function that doubles the value of an integer
def double(x):
return 2 * x
# apply the function to the 'Age' column
df['Age'] = df['Age'].apply(double)
print(df)
In this example, we’re applying the ‘double’ function to the ‘Age’ column of the DataFrame. The function simply multiplies the value by 2. The result is a new DataFrame where the ‘Age’ values have been doubled.
The apply() function can be used with any function that accepts a single argument, and can be used to apply any kind of transformation to a column. This makes it a very powerful tool for data manipulation and cleaning.
Applying a Function to each Row using apply()
Another powerful use case for the Pandas apply() function is applying a function to each row of a DataFrame. This is often used to create new columns based on the values in existing columns, or to perform some kind of transformation or calculation using the values in multiple columns.
To apply a function to each row using apply(), we need to first specify the axis argument as 1. This will ensure that the function is applied to each row instead of each column. We can then define a function that accepts a single argument, which will be a Series object that contains the values for each column in the row.
Here’s an example of how we can apply a function to each row of a Pandas DataFrame:
import pandas as pd
# create a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Salary': [50000, 60000, 70000]
}
df = pd.DataFrame(data)
# define a function that calculates the hourly rate for a salary
def hourly_rate(row):
hourly = row['Salary'] / 2080
return hourly
# apply the function to each row
df['Hourly Rate'] = df.apply(hourly_rate, axis=1)
print(df)
In this example, we’re applying the ‘hourly_rate’ function to each row of the DataFrame. The function calculates the hourly rate for a given salary assuming a 40hour work week (2080 hours per year). The result is a new column called ‘Hourly Rate’ that contains the calculated hourly rates.
The apply() function can be used with any function that accepts a Series object as its argument, and can be used to apply any kind of transformation or calculation to each row of a DataFrame. This makes it a very powerful tool for data analysis and data manipulation.
Applying a Function to Groups using apply()
The Pandas apply() function can also be used to apply a function to groups of rows in a DataFrame. This is a very powerful feature that allows us to perform calculations or transformations on subsets of a DataFrame based on some grouping variable.
To group the rows of a DataFrame, we use the groupby() function. This function allows us to specify one or more columns as the grouping variables, and then create groups based on the unique values in those columns. Once we have created the groups, we can apply a function to each group using the apply() function.
Here’s an example of how we can apply a function to groups of rows in a Pandas DataFrame:
import pandas as pd
# create a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35, 28, 32, 36],
'Salary': [50000, 60000, 70000, 55000, 65000, 75000]
}
df = pd.DataFrame(data)
# define a function that calculates the average salary for a group
def avg_salary(group):
return group['Salary'].mean()
# group the DataFrame by Name and apply the function to each group
grouped = df.groupby('Name').apply(avg_salary)
print(grouped)
In this example, we’re grouping the DataFrame by the ‘Name’ column and applying the ‘avg_salary’ function to each group. The function calculates the average salary for each group. The result is a new DataFrame that contains the average salary for each unique name.
The apply() function can be used with any function that accepts a DataFrame or a Series object as its argument, and can be used to apply any kind of calculation or transformation to groups of rows in a DataFrame. This makes it a very powerful tool for data analysis and data manipulation.
Summary
Pandas apply() function is a powerful tool for data manipulation and cleaning. It can be used to apply a function to a single column, each row, or groups of rows in a DataFrame. With apply(), complex data transformations can be performed easily and efficiently. By using apply() in your data analysis workflows, you can save time and get faster insights from your data.
Related Posts

The Ultimate Python Pandas Guide
By: Adam RichardsonIn this ultimate guide, you will learn how to use Pandas to perform various data manipulation tasks, such as cleaning, filtering, sorting and aggregating data.

A StepbyStep Guide to Joining Pandas DataFrames
By: Adam RichardsonLearn how to join pandas DataFrames efficiently with this stepbystep guide. Improve your data analysis skills and optimize your workflow today!

Appending DataFrames in Pandas: A Tutorial
By: Adam RichardsonLearn how to combine two DataFrames in Pandas using the Append function. This tutorial will guide you on how to join multiple DataFrames with code examples.

Calculating Mean Value Using mean() Function in Pandas
By: Adam RichardsonLearn how to use the mean() function in pandas to calculate the mean value of a dataset in Python. Improve your data analysis skills with this tutorial.