How to Add and Remove Rows and Columns in Pandas DataFrames
Introduction
In this article we walk through the most common pandas operations for modifying the shape of a DataFrame: adding and dropping columns, combining column values, and inserting or removing rows. The examples use a small "people" DataFrame with first name, last name, and email columns.
Adding Columns
- Create a new Series by concatenating existing columns:
python full_name = df["first_name"] + " " + df["last_name"] - Assign the Series to a new column using bracket notation (dot notation does not work for assignment):
python df["full_name"] = full_name - You can also generate a column with
applyfor more complex calculations.
Removing Columns
- Use
dropwithcolumns=and a list of column names:python df.drop(columns=["first_name", "last_name"], inplace=True) inplace=Truemakes the change permanent; otherwisedropreturns a new DataFrame.
Splitting a Column Back into Two
- Apply the string
splitmethod withexpand=Trueto turn a single column into two separate columns:python df[["first_name", "last_name"]] = df["full_name"].str.split(" ", expand=True) - The left‑hand side uses a list of new column names inside brackets.
Adding Rows
Single Row
appenda dictionary (or Series) and setignore_index=Trueso pandas creates a new index automatically:python df = df.append({"first_name": "Tony"}, ignore_index=True)- Missing columns are filled with
NaN.
Appending Another DataFrame
- Create a second DataFrame (
df2) and append it:python df = df.append(df2, ignore_index=True, sort=False) sort=Falsesuppresses the warning about column ordering.- Like column drops,
appenddoes not have aninplaceargument; assign the result back todf.
Removing Rows
- Drop by index:
python df.drop(index=4, inplace=True) - Conditional drop using a boolean filter:
python filt = df["last_name"] == "Doe" df.drop(index=df[filt].index, inplace=True) - Storing the filter in a variable improves readability.
Quick Recap
- Add column → create Series →
df["new"] = series - Remove column →
df.drop(columns=[...], inplace=True) - Split column →
df[["col1","col2"]] = df["col"].str.split(" ", expand=True) - Add row →
df.append(dict, ignore_index=True)(orpd.concat) - Remove row →
df.drop(index=..., inplace=True)or conditional filter.
Why These Operations Matter
Manipulating the structure of a DataFrame is essential for cleaning raw data, preparing features for analysis, and merging datasets. Mastery of these basics speeds up any data‑science workflow.
By mastering df["col"] = …, drop, str.split, and append (or concat), you can reshape pandas DataFrames on the fly, making data cleaning and feature engineering far more efficient.
Frequently Asked Questions
Who is Corey Schafer on YouTube?
Corey Schafer is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.
Does this page include the full transcript of the video?
Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.
Why These Operations Matter
Manipulating the structure of a DataFrame is essential for cleaning raw data, preparing features for analysis, and merging datasets. Mastery of these basics speeds up any data‑science workflow. By mastering `df["col"] = …`, `drop`, `str.split`, and `append` (or `concat`), you can reshape pandas DataFrames on the fly, making data cleaning and feature engineering far more efficient.
Helpful resources related to this video
If you want to practice or explore the concepts discussed in the video, these commonly used tools may help.
Links may be affiliate links. We only include resources that are genuinely relevant to the topic.