Locked lesson.
About this lesson
Let's discuss how to use the drop function to remove a column from your data frame.
Exercise files
Download this lesson’s related exercise files.
Removing Columns.docx58.7 KB Removing Columns - Solution.docx
55.4 KB
Quick reference
Removing Columns
Removing Columns can be done temporarily or permanently.
When to use
Use these methods whenever you want to delete a column permanently or temporarily.
Instructions
To remove a column, use the drop function.
my_df.drop("Total", axis=1, inplace=True)
The first argument ("Total") is the column you want to delete.
The second argument ("axis=1") designates what to delete. Axis=1 is for columns, axis=0 is for rows.
The third argument is whether or not you want the deletion to be permanent. Inplace=True is for permanent, false is for temporary.
Hints & tips
- my_df.drop("Total", axis=1, inplace=True)
- 00:05 Okay, so he added a couple of new columns, the Sunday column and the Total column.
- 00:08 We're very happy with the total column but then we realize,
- 00:11 well, we don't really need this Sun column.
- 00:13 So what do we do to get rid of it?
- 00:15 Well, it's actually pretty simple.
- 00:16 We can use the drop function.
- 00:18 So let's go my_df.drop and
- 00:23 then inside of here, we could just designate what do we want to drop.
- 00:27 So if we type in Sunday and run this, we'll actually get an error.
- 00:32 And if we scroll down here we see Sun is not found in axis.
- 00:36 So what does that mean?
- 00:37 Well, this top column headers, that's one axis and
- 00:41 the sort of index row headers, that's the other axis.
- 00:46 And these are numericals, so this top one is the one axis and
- 00:50 the row headings that's the zero axis.
- 00:54 So, if we come up here and
- 00:55 hit Shift Enter, we can see axis is zero is the default.
- 01:00 So by default, it's looking for Sunday in these A, B, C,
- 01:04 D row labels, and it can't find Sunday in there because it doesn't exist.
- 01:08 So all we have to do is just slap in a comma and designate the axis to be one.
- 01:14 And remember one is the column headers.
- 01:17 So now if we Shift Enter to run this, boom that works, and Sunday is gone.
- 01:22 So, you're probably going to forget the axis thing in the future.
- 01:26 I do it every once in a while.
- 01:27 You'll just get an error and you'll go, yeah, I forgot to designate the axis.
- 01:30 And it's not really intuitive that the top column headers
- 01:35 are the one axis and the rows are the zero axis.
- 01:39 So you may have to play around with it to jog your memory but it's not too bad.
- 01:43 So now here's an interesting thing, we can call our data frame again.
- 01:48 And when we do, Sunday is popped back up again.
- 01:52 I thought we just deleted it.
- 01:53 Well, we sort of did and we sort of didn't, right?
- 01:56 Pandas has something that's, I don't know, think of it as error handling, right?
- 02:01 I can't tell you how many times I've deleted data that I didn't
- 02:04 actually want to delete and man, you just hate to do that.
- 02:07 And pandas sort of knows that that happens so, like I said,
- 02:10 this sort of built-in error handling,
- 02:12 it will only delete things if you specify that yes, in fact, we want to delete it.
- 02:17 And does this was something called an in place command.
- 02:20 And basically think of do we want to do this in place?
- 02:23 Do we want it to happen right here permanently?
- 02:27 So we can designate in place equals true and
- 02:32 when we designate this to equal true, it will be deleted forever.
- 02:36 And notice this is not in quotation marks or anything like that, it's just true.
- 02:41 This is a boolean like true and false.
- 02:43 So now, if we Shift Enter to run this and
- 02:46 then come down here to my_df again and Shift Enter to run this.
- 02:52 Now we see Sunday is gone for good.
- 02:55 Remember this in place thing.
- 02:57 Pandas uses this for a lot of things.
- 02:59 So going forward, a lot of times when we want to do something permanent,
- 03:02 we almost always have to designate it as in place equals true.
- 03:06 And it can be kind of a hassle but
- 03:08 it's actually a good thing because it's just sort of a little safety net so
- 03:12 you're not deleting things accidentally that you don't want to delete.
- 03:15 And you know, if you think about it, that's really a good thing.
- 03:17 And it's not such a bad thing to have to type in in place equals true, just to
- 03:22 make sure that you don't accidentally delete things you don't want to delete.
- 03:25 So that's how to delete columns.
- 03:27 Pretty straightforward.
- 03:27 Just remember your axis.
- 03:29 The top column is axis one, and
- 03:32 the, up and down, the horizontal rows, that's axis zero.
- 03:37 And that's all there is to it.
- 03:38 So in the next video we'll look at deleting rows
Lesson notes are only available for subscribers.