Editor

Row-Level Apply

Advanced · Write the code · from Pandas apply(), map(), transform(): Custom Data Transformations

From the lesson: Pandas apply(), map(), transform(): Custom Data Transformations

Pandas gives you dozens of built-in methods like .sum(), .mean(), and .str.upper(). But what happens when you need to run your own custom logic on every row, column, or cell? That is where apply(), map(), and transform() come in.

Read the lesson

Use apply() with axis=1 to create a new column label in the DataFrame. The label should be the name followed by the age in parentheses, like "Alice (30)".

Print the label column as a list.

import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Carol'],
'age': [30, 25, 35]
})
# Create a label column using apply with axis=1

More Pandas Apply and Transform exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz