Adding, Modifying & Dropping Columns
Modifying columns means reshaping a DataFrame's structure — creating new columns from existing data, renaming them, and removing the ones you no longer need — so your table holds exactly the information your analysis requires.
Learn Adding, Modifying & Dropping Columns in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Learn bracket assignment, derived columns, df.assign(), rename(), drop(), and insert() — the everyday toolkit for transforming tables.
The simplest way to add a column is to assign to a new name: df["new"] = ... . If the name doesn't exist yet, pandas creates it; if it does, pandas overwrites it. The right side can be a scalar, a list, or — most powerfully — a calculation over existing columns .
df.assign(new=...) returns a new DataFrame with the column added, leaving the original untouched — ideal for method chaining. df.rename(columns={' '}) changes column names using an old-to-new mapping.
df.drop(columns=[...]) removes columns; df.drop(index=[...]) removes rows by their index label. Both return a new DataFrame by default. Use df.insert(loc, name, values) when you need a new column at a specific position instead of the far right.
Transform an orders DataFrame using every tool from this lesson.
Lesson complete — you can reshape any table!
You can add columns by assignment, derive them from existing data, use assign() for safe chaining, rename with a mapping, drop columns and rows, and place columns precisely with insert() .
🚀 Up next: Handling Missing Data — deal with NaN using fillna and dropna.
Practice quiz
What does df['new'] = ... do if 'new' does not exist yet?
- Raises a KeyError
- Creates the new column
- Returns a copy
- Renames a column
Answer: Creates the new column. Bracket assignment creates the column if the name is new, or overwrites it if it exists.
What does df['price'] * df['qty'] do?
- Multiplies row by row (vectorized), no loop
- Concatenates strings
- Needs a for loop
- Raises an error
Answer: Multiplies row by row (vectorized), no loop. Vectorized math multiplies the two columns element-wise automatically.
What does df.assign(new=...) return?
- None
- Just the new column
- A new DataFrame with the column added
- The original mutated frame
Answer: A new DataFrame with the column added. assign returns a new DataFrame and leaves the original untouched — great for chaining.
How do you rename columns with df.rename?
- a
- b
Pass a columns mapping dict of old name to new name.
What does df.drop(columns=['x']) return by default?
- A new DataFrame without column x
- None
- Only column x
- An error
Answer: A new DataFrame without column x. drop returns a new frame by default; the original is unchanged unless you reassign or use inplace.
Which argument drops rows by their index label?
- columns=
- axis='rows'
- index=
- subset=
Answer: index=. df.drop(index=[...]) removes rows by index label; columns=[...] removes columns.
When does df.insert() matter over plain bracket assignment?
- When you need the column at a specific position
- When mutating is forbidden
- When the column is numeric
- Never
Answer: When you need the column at a specific position. insert(loc, name, values) places a column at a chosen index; brackets always append at the far right.
Does df.insert(0, 'id', vals) mutate the DataFrame in place?
- No, it returns a copy
- Only if inplace=True
- Yes, it mutates in place
- It raises an error
Answer: Yes, it mutates in place. insert modifies the DataFrame in place and errors if the name already exists.
Why might a column from df.assign(...) seem to disappear?
- assign deletes columns
- The result wasn't captured (assign doesn't mutate)
- assign needs inplace
- Columns expire
Answer: The result wasn't captured (assign doesn't mutate). assign returns a copy; if you don't store it, the original df is unchanged.
If you do df['total'] = df['price'] * df['qty'], where does 'total' appear?
- At position 0
- It replaces 'price'
- It is hidden
- Appended at the far right
Answer: Appended at the far right. Plain bracket assignment always adds the new column at the right end.