0
0
Data Analysis Pythondata~5 mins

apply() function for custom logic in Data Analysis Python

Choose your learning style9 modes available
Introduction

The apply() function helps you run your own rules on each row or column of data. It makes changing or analyzing data easy and flexible.

You want to add a new column based on calculations from other columns.
You need to clean or change data values with your own rules.
You want to summarize or transform data in a custom way.
You want to apply a function to each row or column without writing loops.
You want to quickly test small changes on your data.
Syntax
Data Analysis Python
DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwargs)

func is your custom function to apply.

axis=0 applies function to each column, axis=1 applies to each row.

Examples
Adds 1 to every value in each column.
Data Analysis Python
df.apply(lambda x: x + 1)
Creates a sum of columns A and B for each row.
Data Analysis Python
df.apply(lambda row: row['A'] + row['B'], axis=1)
Doubles each value in column A using a named function.
Data Analysis Python
def double(x):
    return x * 2

df['A'].apply(double)
Sample Program

This program creates a table of students and their scores. It uses apply() to add a new column with the average score for each student.

Data Analysis Python
import pandas as pd

data = {'Name': ['Anna', 'Bob', 'Cara'],
        'Math': [80, 90, 70],
        'English': [85, 88, 78]}

df = pd.DataFrame(data)

# Define a function to calculate average score
def average_score(row):
    return (row['Math'] + row['English']) / 2

# Apply the function to each row
df['Average'] = df.apply(average_score, axis=1)

print(df)
OutputSuccess
Important Notes

Using axis=1 applies the function to each row, which is common for custom logic involving multiple columns.

Functions used with apply() should return a single value for each row or column.

For large data, apply() can be slower than vectorized operations, so use it when custom logic is needed.

Summary

apply() lets you run your own function on rows or columns easily.

Use axis=1 for row-wise and axis=0 for column-wise operations.

Great for adding new columns or transforming data with custom rules.