Last updated
Adding & Modifying Columns
Definition: You create or change a column simply by assigning to it. Calculations apply to the whole column at once, thanks to NumPy underneath.
Example 1 — a new calculated column
import pandas as pd
df = pd.DataFrame({
"price": [10, 20, 30],
"qty": [2, 3, 4]
})
df["total"] = df["price"] * df["qty"]
print(df)
Example 2 — update an existing column
import pandas as pd
df = pd.DataFrame({"price": [10, 20, 30]})
df["price"] = df["price"] * 1.1 # add 10%
print(df)
Example 3 — remove a column
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
df = df.drop(columns=["b"])
print(df)
💡 Tip: creating a column from a calculation on other columns is the heart of data work — for example, revenue = price x quantity.
Try it Yourself
Output
Ad · responsive