Skip to content
Justice Tech School

apply on Rows

apply with axis=1 runs a function per row.

Example
df = pd.DataFrame({"name": ["Ada", "Kofi", "Amina", "Tunde"], "country": ["NG", "GH", "NG", "NG"], "score": [92, 78, 85, 71]})
df["label"] = df.apply(lambda r: r["name"] + " (" + r["country"] + ")", axis=1)
print(df["label"].tolist())

Result

['Ada (NG)', 'Kofi (GH)', 'Amina (NG)', 'Tunde (NG)']

Exercise

Per row.

df.apply(f, =1)

Quiz Test

What does axis=1 mean in apply?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.