Locked lesson.
About this lesson
Deleting a row in a data frame is very similar to deleting a column - but with one small difference.
Exercise files
Download this lesson’s related exercise files.
Deleting Rows.docx57.1 KB Deleting Rows - Solution.docx
55.5 KB
Quick reference
Deleting Rows
Deleting rows is very similar to deleting columns. Use the drop() function.
When to use
Use this method to delete rows in your DataFrame.
Instructions
To delete a row named "C" from a DataFrame name my_df:
my_df.drop("C", inplace=True)
You don't have to designate an axis, because by default the 0 axis is the default axis in the drop() function, and rows are axis=0.
Hints & tips
- my_df.drop("C", inplace=True)
- 00:05 Okay, in the last video, we looked at how to remove columns.
- 00:08 In this video, we're going to look at how to remove rows.
- 00:11 And it's actually very similar.
- 00:13 In fact, it's almost exactly the same, but
- 00:15 there's some slight variations that make it worth talking about.
- 00:18 So that's what we're going to do in this video.
- 00:20 So, okay, we want to get rid of a row.
- 00:22 And let's say C, I really don't like the look of C, we need to get rid of it.
- 00:26 How do we do that?
- 00:26 Well, we just call my_df.drop, and
- 00:30 then we just pass in whatever row we want.
- 00:34 Now, remember in the last video when we tried to do this with just putting Sunday
- 00:38 in, we got an error.
- 00:40 If we do this in this video with a row, we don't get an error.
- 00:44 Why is that?
- 00:45 Well, remember our axis, these column headers are axis 1,
- 00:50 the row headings are axis 0.
- 00:53 And if we Shift+Tab right here, we can see the default is axis 0.
- 01:00 So if we're going to be removing something from axis 0,
- 01:05 we don't actually have to type in axis 0, because it's the default.
- 01:09 So that's kind of interesting.
- 01:11 I have mixed feelings about that.
- 01:13 I think, especially for beginners, you should just get in the habit of typing in
- 01:17 axis=0 anyway, even though you don't have to, why?
- 01:23 Because axes aren't really intuitive, right?
- 01:26 You're going to forget that these top columns are axis 1,
- 01:29 you're going to forget that these rows are axis 0.
- 01:32 But if you get in the habit of always typing in axis,
- 01:35 you're less likely to forget over time.
- 01:38 So I would suggest that you always type in your axis.
- 01:41 Now again, we can come down here and go my_df and run this again,
- 01:46 and our troublesome C row is puffed back up.
- 01:50 That's because we didn't designate
- 01:53 right here that we wanted to do this in place, right?
- 01:56 So we can always go inplace=True.
- 02:00 And now if we run this and
- 02:04 then run our data frame again, now C has been deleted forever more, right?
- 02:10 So that's how to drop rows.
- 02:12 Pretty simple.
- 02:12 Now, I mentioned in the last video that we like this inplace=True thing,
- 02:17 because it keeps us from deleting things that we might not want to delete.
- 02:21 And when you do use inplace=True, it deletes it forever, but not really.
- 02:27 I mean, yes, but no, there's a kind of a hacky way around that.
- 02:30 And I want to show you that right now.
- 02:32 As long as your notebook is still open, and you've got all these old cells, right?
- 02:37 Even though it's been deleted down here,
- 02:39 we can always come back up to the top and just run all these cells again.
- 02:43 And it will generate our deleted stuff again, right?
- 02:46 So I'm just going to hold down the Shift key,
- 02:49 click on one of these cells and hit Enter.
- 02:52 And I'm continuing to hold down the Shift key, and I'll just keep hitting Enter,
- 02:56 Enter, Enter, Enter.
- 03:00 And we can come down, Enter, Enter, Enter.
- 03:06 Now, if we Shift run this one again, boom, our C has come back.
- 03:10 Now these numbers will have sort of changed a little bit,
- 03:13 that's because up here remember, we created all these numbers randomly.
- 03:17 So if we run these cells again, it will generate new random numbers, which may or
- 03:21 may not be what we want.
- 03:23 We can get around that if we use a seed, we didn't actually use a seed, and
- 03:26 I didn't even talk about it.
- 03:27 But when you create a random number, you can issue a seed and
- 03:32 if you had done that, then it will generate the same random numbers if
- 03:35 you use that same seed in the future.
- 03:37 Sort of beyond the scope all of this, we don't really care about that, but
- 03:40 just something to keep in mind.
- 03:41 If you were using actual data and you weren't creating random numbers, well,
- 03:45 then if you ran these cells again, in that case, they would be the same.
- 03:49 And then down here, your numbers would still be the same and
- 03:52 you'd have your C row back.
- 03:54 So kind of a hacky way around it.
- 03:55 I mean, it's not a great thing to do.
- 03:57 But in a pinch, if you accidentally inplace deleted something you didn't
- 04:01 want to delete, you could always scroll back up.
- 04:03 And it's kind of one of the nice things about Jupyter Notebooks.
- 04:06 It has all that stuff there as long as you haven't deleted it.
- 04:10 As we're going along, I'm sort of deleting somethings just to clean things up.
- 04:14 As long as you haven't done that, you're good to go.
- 04:16 So that's all for deleting rows, deleting columns.
- 04:19 In the next video,
- 04:20 I want to show you how to select rows, couple of ways that you can select rows.
- 04:23 So that you can actually grab data and start doing stuff with it, and
- 04:27 that'll be in the next video.
Lesson notes are only available for subscribers.