How to use the apply method with Pandas in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
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 :
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚