0
0
Pandasdata~5 mins

apply() on rows (axis=1) in Pandas

Choose your learning style9 modes available
Introduction

We use apply() with axis=1 to run a function on each row of a table. This helps us create new data or change existing data based on row values.

You want to calculate a new column based on values from multiple columns in the same row.
You need to check conditions across columns for each row and assign a label or category.
You want to combine or transform row data into a summary or string.
You want to apply a custom function that looks at all columns in a row to decide a result.
Syntax
Pandas
DataFrame.apply(function, axis=1)

function is a function you write that takes a row (a Series) and returns a value.

axis=1 tells pandas to apply the function across rows, not columns.

Examples
Adds values from columns 'A' and 'B' for each row.
Pandas
df.apply(lambda row: row['A'] + row['B'], axis=1)
Creates a new column 'result' labeling each row as 'Pass' or 'Fail' based on 'score'.
Pandas
def label_row(row):
    if row['score'] > 50:
        return 'Pass'
    else:
        return 'Fail'
df['result'] = df.apply(label_row, axis=1)
Sample Program

This code creates a table with names and scores. It then calculates the average score for each student by applying a function on each row. The result is saved in a new column 'average'.

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob', 'Charlie'],
        'math': [80, 45, 70],
        'english': [75, 55, 65]}
df = pd.DataFrame(data)

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

# Apply function on rows
df['average'] = df.apply(average_score, axis=1)

print(df)
OutputSuccess
Important Notes

Using apply() with axis=1 can be slower on big data. For simple math, vectorized operations are faster.

The function you apply receives each row as a pandas Series, so you access columns by name like row['column_name'].

Summary

apply(axis=1) runs a function on each row of a DataFrame.

It helps create new columns or transform data based on multiple columns in the same row.

Good for custom row-wise calculations or conditions.