How do I remove columns from a pandas DataFrame?

Data School · Beginner ·📰 AI News & Updates ·10y ago

Key Takeaways

This video demonstrates how to remove columns and rows from a pandas DataFrame using the drop method, explaining the axis and inplace parameters.

Full Transcript

hello and welcome back to my video Q&A series about the Panda's library in Python so the question for today is how do I remove columns from a panda's data frame great question uh so the panda data frame is of course the primary unit of uh data storage in the pandas Library it's like uh rows and columns so how do I remove a column uh as always we are going to pull up an example data set and uh but first we need to import pandas as PD and then uh we're going to pull up the UFO data set a data set of uh UFO sightings or reported sightings and so we'll use the read CSV function and we'll pass it the URL of the data set bit. Le slash UFO reports okay so this creates a pandas data frame and uh we'll just look at the head as a reminder of what is in this data frame they're just UFO reports it has uh five columns and in case you're curious you can use the shape attribute and see that there are 18,2 41 rows that's what this is telling us anyway uh let's pretend that we don't care about the colors reported column so we want to get rid of it okay how do we do that well let's uh we'll just use the drop method which is a data frame method so you say ufo. drop and then we're just going to pass it as a string the name of the column we want to drop so colors reported now there are two more things I'm going to add here the first is the axis so I'm going to say axis equals 1 and we'll talk more about axes in in another video but um in brief an axis is just kind of a direction okay so axis zero is the row axis um it kind of is a d like directionally like down is Axis zero I think of it is the row axis a AIS one is the column axis it's the one that kind of moves across and that movement will make more sense uh when we talk um more about it in a future video but axis equals 1 is the way I say to pandas I want to drop a column and its name is colors reported okay one more thing we're going to say in place equals true what does that mean uh it just means I want this operation to occur in place okay so uh we will run this and uh we will just rerun the head method and you will see that the colors reported column AKA series is now missing it's now gone okay so um you can actually drop multiple columns at once uh so I'll show you how to do that now so let's say I wanted to get rid of city and state I didn't care about those let's do ufo. drop and what you do is very similar except you just instead of passing a string you pass a list of strings okay so I'll say City uh whoops comma State okay so the first argument is that list the second argument is X is equal 1 third argument in place equals true we run that and UFO do head is now down to those two columns okay they are gone they are no longer part of the data frame and that's really it okay as always I like to end these videos with a bonus tip and the bonus tip is uh what if you want to remove uh rows instead of columns Well turns out you just use the drop method except you use axis zero cuz as I said axis one is the column axis axis zero is the row axis okay so uh so we're going to use ufo. drop and we know there's going to be something here and then we're going to say axis equals Zer and in place equals true but what is this first thing let's say I want to drop uh rows the first two rows because I don't care about UFO reports before 19 18 31 so I don't care about these two rows I don't want them in the data set so how do I refer to them um by name I actually the names are right here the names are zero and one okay just like these are the names of the columns these are the names of the rows they're uh officially called the index uh also called the labels and so I'm just going to pass a list and I'll just say Z comma 1 run that ufo. head and you you can see those two rows the 1930s have been dropped and if you really want to be sure that they're gone you can see that the shape uh the number of rows has decreased by two rows okay so uh one final note about this axis equals z is actually the default so technically I did not have to specify axis equals Z here uh however I always like to specify the axis just to be really clear about what I am doing okay so that's all for today thanks as always for joining me uh please click subscribe if you'd like to see more videos like this as always leave a comment uh telling me your question about this video or your Panda's question in general I will uh try to answer it maybe I'll even make a video okay so that's it uh thanks again and I will see you again soon

Original Description

If you have DataFrame columns that you're never going to use, you may want to remove them entirely in order to focus on the columns that you do use. In this video, I'll show you how to remove columns (and rows), and will briefly explain the meaning of the "axis" and "inplace" parameters. SUBSCRIBE to learn data science with Python: https://www.youtube.com/dataschool?sub_confirmation=1 JOIN the "Data School Insiders" community and receive exclusive rewards: https://www.patreon.com/dataschool == RESOURCES == GitHub repository for the series: https://github.com/justmarkham/pandas-videos "drop" documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html == LET'S CONNECT! == Newsletter: https://www.dataschool.io/subscribe/ Twitter: https://twitter.com/justmarkham Facebook: https://www.facebook.com/DataScienceSchool/ LinkedIn: https://www.linkedin.com/in/justmarkham/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Data School · Data School · 27 of 60

1 Setting up Git and GitHub
Setting up Git and GitHub
Data School
2 Navigating a GitHub Repository - Part 1
Navigating a GitHub Repository - Part 1
Data School
3 Forking a GitHub Repository
Forking a GitHub Repository
Data School
4 Creating a New GitHub Repository
Creating a New GitHub Repository
Data School
5 Copying a GitHub Repository to Your Local Computer
Copying a GitHub Repository to Your Local Computer
Data School
6 Committing Changes in Git and Pushing to a GitHub Repository
Committing Changes in Git and Pushing to a GitHub Repository
Data School
7 Syncing Your GitHub Fork
Syncing Your GitHub Fork
Data School
8 Allstate Purchase Prediction Challenge on Kaggle
Allstate Purchase Prediction Challenge on Kaggle
Data School
9 Troubleshooting: Updates Rejected When Pushing to GitHub
Troubleshooting: Updates Rejected When Pushing to GitHub
Data School
10 Hands-on dplyr tutorial for faster data manipulation in R
Hands-on dplyr tutorial for faster data manipulation in R
Data School
11 ROC Curves and Area Under the Curve (AUC) Explained
ROC Curves and Area Under the Curve (AUC) Explained
Data School
12 Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Data School
13 What is machine learning, and how does it work?
What is machine learning, and how does it work?
Data School
14 Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Data School
15 Getting started in scikit-learn with the famous iris dataset
Getting started in scikit-learn with the famous iris dataset
Data School
16 Training a machine learning model with scikit-learn
Training a machine learning model with scikit-learn
Data School
17 Comparing machine learning models in scikit-learn
Comparing machine learning models in scikit-learn
Data School
18 Data science in Python: pandas, seaborn, scikit-learn
Data science in Python: pandas, seaborn, scikit-learn
Data School
19 Selecting the best model in scikit-learn using cross-validation
Selecting the best model in scikit-learn using cross-validation
Data School
20 How to find the best model parameters in scikit-learn
How to find the best model parameters in scikit-learn
Data School
21 How to evaluate a classifier in scikit-learn
How to evaluate a classifier in scikit-learn
Data School
22 What is pandas? (Introduction to the Q&A series)
What is pandas? (Introduction to the Q&A series)
Data School
23 How do I read a tabular data file into pandas?
How do I read a tabular data file into pandas?
Data School
24 How do I select a pandas Series from a DataFrame?
How do I select a pandas Series from a DataFrame?
Data School
25 Why do some pandas commands end with parentheses (and others don't)?
Why do some pandas commands end with parentheses (and others don't)?
Data School
26 How do I rename columns in a pandas DataFrame?
How do I rename columns in a pandas DataFrame?
Data School
How do I remove columns from a pandas DataFrame?
How do I remove columns from a pandas DataFrame?
Data School
28 How do I sort a pandas DataFrame or a Series?
How do I sort a pandas DataFrame or a Series?
Data School
29 How do I filter rows of a pandas DataFrame by column value?
How do I filter rows of a pandas DataFrame by column value?
Data School
30 How do I apply multiple filter criteria to a pandas DataFrame?
How do I apply multiple filter criteria to a pandas DataFrame?
Data School
31 Your pandas questions answered!
Your pandas questions answered!
Data School
32 How do I use the "axis" parameter in pandas?
How do I use the "axis" parameter in pandas?
Data School
33 How do I use string methods in pandas?
How do I use string methods in pandas?
Data School
34 How do I change the data type of a pandas Series?
How do I change the data type of a pandas Series?
Data School
35 When should I use a "groupby" in pandas?
When should I use a "groupby" in pandas?
Data School
36 How do I explore a pandas Series?
How do I explore a pandas Series?
Data School
37 How do I handle missing values in pandas?
How do I handle missing values in pandas?
Data School
38 What do I need to know about the pandas index? (Part 1)
What do I need to know about the pandas index? (Part 1)
Data School
39 What do I need to know about the pandas index? (Part 2)
What do I need to know about the pandas index? (Part 2)
Data School
40 How do I select multiple rows and columns from a pandas DataFrame?
How do I select multiple rows and columns from a pandas DataFrame?
Data School
41 Machine Learning with Text in scikit-learn (PyCon 2016)
Machine Learning with Text in scikit-learn (PyCon 2016)
Data School
42 When should I use the "inplace" parameter in pandas?
When should I use the "inplace" parameter in pandas?
Data School
43 How do I make my pandas DataFrame smaller and faster?
How do I make my pandas DataFrame smaller and faster?
Data School
44 How do I use pandas with scikit-learn to create Kaggle submissions?
How do I use pandas with scikit-learn to create Kaggle submissions?
Data School
45 More of your pandas questions answered!
More of your pandas questions answered!
Data School
46 How do I create dummy variables in pandas?
How do I create dummy variables in pandas?
Data School
47 How do I work with dates and times in pandas?
How do I work with dates and times in pandas?
Data School
48 How do I find and remove duplicate rows in pandas?
How do I find and remove duplicate rows in pandas?
Data School
49 How do I avoid a SettingWithCopyWarning in pandas?
How do I avoid a SettingWithCopyWarning in pandas?
Data School
50 How do I change display options in pandas?
How do I change display options in pandas?
Data School
51 How do I create a pandas DataFrame from another object?
How do I create a pandas DataFrame from another object?
Data School
52 How do I apply a function to a pandas Series or DataFrame?
How do I apply a function to a pandas Series or DataFrame?
Data School
53 Getting started with machine learning in Python (webcast)
Getting started with machine learning in Python (webcast)
Data School
54 Q&A about Machine Learning with Text (online course)
Q&A about Machine Learning with Text (online course)
Data School
55 Your pandas questions answered! (webcast)
Your pandas questions answered! (webcast)
Data School
56 Machine Learning with Text in scikit-learn (PyData DC 2016)
Machine Learning with Text in scikit-learn (PyData DC 2016)
Data School
57 Write Pythonic Code for Better Data Science (webcast)
Write Pythonic Code for Better Data Science (webcast)
Data School
58 Web scraping in Python (Part 1): Getting started
Web scraping in Python (Part 1): Getting started
Data School
59 Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Data School
60 Web scraping in Python (Part 3): Building a dataset
Web scraping in Python (Part 3): Building a dataset
Data School

This video teaches how to remove columns and rows from a pandas DataFrame using the drop method, covering the axis and inplace parameters. It provides a clear understanding of data manipulation using pandas.

Key Takeaways
  1. Import pandas as PD
  2. Load a sample data set using read CSV function
  3. Use the drop method to remove a column
  4. Specify the axis parameter to remove a column or row
  5. Use the inplace parameter to modify the original DataFrame
  6. Verify the changes using the head method
💡 The axis parameter is crucial in specifying whether to remove a column or row, and the inplace parameter determines whether to modify the original DataFrame.

Related AI Lessons

The AI Moat Paradox: The Better Models Become, the Less Models Matter
The AI moat paradox suggests that as AI models improve, their importance may decrease, and understanding this concept is crucial for AI professionals and businesses.
Medium · AI
170,927 AI Papers Reveal the Biggest Research Shifts of the First Half of 2026
Discover the biggest AI research shifts of 2026 based on 170,927 papers, and learn how to apply these trends to your work
Medium · Machine Learning
170,927 AI Papers Reveal the Biggest Research Shifts of the First Half of 2026
Discover the major research shifts in AI from 170,927 papers published in the first half of 2026, and learn how to analyze trends in AI research
Medium · Data Science
[PoV] When Everyone Is Smart, No One Is
In a world where AI makes everyone smart, the value of intelligence decreases, and new challenges arise
Medium · AI
Up next
Man dies after horror Gold Coast house fire; high-speed Sydney motorway pursuit | 9 News Australia
9 News Australia
Watch →