0
0
Pandasdata~3 mins

Why apply() on columns in Pandas? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could update every column in your data with just one simple command?

The Scenario

Imagine you have a big table of sales data in a spreadsheet. You want to change every price by adding tax or convert all dates to a new format. Doing this by clicking and typing for each column feels like a never-ending chore.

The Problem

Manually editing each cell or column is slow and tiring. It's easy to make mistakes, like forgetting a cell or typing wrong numbers. When the data grows, this manual work becomes impossible to finish on time.

The Solution

The apply() function lets you quickly run a small task on every column in your table. You write the task once, and apply() does the work for all columns automatically. This saves time and avoids errors.

Before vs After
Before
for col in df.columns:
    df[col] = [x * 1.1 for x in df[col]]
After
df = df.apply(lambda col: col * 1.1)
What It Enables

With apply(), you can transform entire columns of data in one simple step, making your work faster and more reliable.

Real Life Example

A store manager wants to increase all product prices by 10% after tax changes. Instead of changing each price manually, they use apply() to update all price columns instantly.

Key Takeaways

Manual edits on columns are slow and error-prone.

apply() runs a function on every column automatically.

This makes data changes faster, easier, and safer.