Adding and Removing Columns
Assign a new column with [] and remove with drop. assign returns a copy.
Example
df = pd.DataFrame({"name": ["Ada", "Kofi", "Amina", "Tunde"], "country": ["NG", "GH", "NG", "NG"], "score": [92, 78, 85, 71]})
df["bonus"] = df["score"] * 0.1
df2 = df.assign(passed=df["score"] >= 75)
print(df2.drop(columns=["bonus"]))Result
name country score passed 0 Ada NG 92 True 1 Kofi GH 78 True 2 Amina NG 85 True 3 Tunde NG 71 False
Exercise
Remove a column.
df.(columns=["a"])
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.