How to use the apply method with Pandas in Python

2 min

If you are working with Pandas DataFrame (I hope that you do because it is an incredible tool for data manipulation, if you haven't you might want to go check out my other DataFrame articles)

You will probably need to do some advanced column/row-wise operations.

So let me present to you the pandas.DataFrame.apply() method.

What is so different from looping over a DataFrame?

It is a faster and more concise way to loop over a DataFrame especially when you only need one column.

For the apply function to work you will have to pass it another function.

Either a normally defined function or a lambda function.

Here are some examples of some specific use cases

Example 1: Multiply by 2

import pandas as pd 

# we define our example dataset 
df = pd.DataFrame({"col1":range(10)})

# we multiply our col1 by two using basic column operation 
df["col1"] = df["col1"] * 2 

# the same using apply and lambda functions 
df["col1"] = df["col1"].apply(lambda x: x*2)

Example 2: Check if an observation equals a specific text

import pandas as pd 

# We define our example dataset 
df = pd.DataFrame({"col_text": ["whatsup","guys","how u doing"]})

# We check if a string is the one we are looking for in our list of string
# Which is not really efficient
df["col_text"] = [True if "guys" in each else False for each in df["col_text"]]

# Using lambda function and the apply method
df["col_text"] = df["col_text"].apply(lambda x: True if "guys" in x else
False)

# Using a simple method we can do the same thing
def check_if_string_matches(x, str_to_match="guys"):
    """Check whether a string contains another"""
    return str_to_match in x
    
# How to use the apply method with a simple function
df["col_text"] = df["col_text"].apply(check_if_string_matches, str_to_match="guys")

Here you are! You now know how to use the pandas apply() function!

More on DataFrames

If you want to know more about DataFrame and Pandas. Check out the other articles I wrote on the topic, just here :

Pandas - The Python You Need
We gathered the only Python essentials that you will probably ever need.