apply and map
apply runs a function on every item and map replaces values with a dictionary.
Example
df = pd.DataFrame({"name": ["Ada", "Kofi", "Amina", "Tunde"], "country": ["NG", "GH", "NG", "NG"], "score": [92, 78, 85, 71]})
print(df["score"].apply(lambda x: x + 5).tolist())
print(df["country"].map({"NG": "Nigeria", "GH": "Ghana"}).tolist())Result
[97, 83, 90, 76] ['Nigeria', 'Ghana', 'Nigeria', 'Nigeria']
Exercise
Run a function.
s.(len)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.