How to Add and Remove Rows and Columns in Pandas DataFrames

 2 min read

YouTube video ID: HQ6XO9eT-fc

Source: YouTube video by Corey SchaferWatch original video

PDF

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 apply for more complex calculations.

Removing Columns

  • Use drop with columns= and a list of column names: python df.drop(columns=["first_name", "last_name"], inplace=True)
  • inplace=True makes the change permanent; otherwise drop returns a new DataFrame.

Splitting a Column Back into Two

  • Apply the string split method with expand=True to 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

  • append a dictionary (or Series) and set ignore_index=True so 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=False suppresses the warning about column ordering.
  • Like column drops, append does not have an inplace argument; assign the result back to df.

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 columndf.drop(columns=[...], inplace=True)
  • Split columndf[["col1","col2"]] = df["col"].str.split(" ", expand=True)
  • Add rowdf.append(dict, ignore_index=True) (or pd.concat)
  • Remove rowdf.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.

PDF