Locked lesson.
About this lesson
There are times when you may need to reset your index within a data frame, so we'll explain how that works in this video.
Exercise files
Download this lesson’s related exercise files.
Changing Indexes.docx57 KB Changing Indexes - Solution.docx
55.5 KB
Quick reference
Changing Indexes
We can reset our index to its original numeric values, and we can create a completely different index with different labels.
When to use
Do this whenever you need to reset your index to numbers, or create a new labelled index.
Instructions
To reset an index to its original numerical values:
my_df.reset_index(inplace=True)
To create a new column and set it as the index:
my_df['New'] = ["E", "F", "G", "H"]
my_df.set_index('New', inplace=True)
Hints & tips
- Reset Index: my_df.reset_index(inplace=True)
- Create New Column: my_df['New'] = ["E", "F", "G", "H"]
- Set New Column as Index: my_df.set_index('New', inplace=True)
- 00:05 Okay, in this video, I want to talk about changing and resetting our indexes.
- 00:09 And by index i mean these row headings A, B, C, and D.
- 00:14 So there are times when you may want to reset your index
- 00:18 back to the original numbers.
- 00:20 Remember what we talked about many videos ago,
- 00:22 these indexes are actually just like Python lists, right?
- 00:25 They start at 0, so 0, 1, 2, 3.
- 00:29 We've got them listed as labels because you often want to have them as labels.
- 00:33 We're humans, we read letters and words probably better,
- 00:38 so usually going to do that.
- 00:40 But there are times when you may need to change these back to their original
- 00:44 index numbers.
- 00:45 So that's the first thing we're going to look at in this video.
- 00:48 After that, we'll look at how to change them completely.
- 00:51 So let's say you want to change the labels to something else.
- 00:53 How do you do that?
- 00:54 So first, let's reset this index.
- 00:57 So we just call my_df.reset_index, and
- 01:02 it's a function.
- 01:07 And that's really all we have to do.
- 01:10 And you'll notice now we have 0, 1, 2, 3 original index numbers as the index.
- 01:16 You'll also notice that our old labels are still here,
- 01:20 they've just been put into a new column called index.
- 01:23 Now, you could drop that index column if you wanted,
- 01:25 and we've learned how to drop columns in the past, so you could do that.
- 01:28 Now, like so many things, if we run our my_df,
- 01:32 it goes back to the way it used to be because we didn't designate here.
- 01:37 If we sort of Shift, Shift and Tab,
- 01:40 we can see it has an inplace requirement, right?
- 01:46 So if we wanted to make this permanent,
- 01:47 we would just inplace=True, like we've done before.
- 01:53 I don't necessarily want to make this permanent, so we'll just get rid of that.
- 01:59 So that's how you reset it to its original index number, it's pretty simple.
- 02:03 If you want to actually change these labels, A,
- 02:07 B, C, and D, to something else, that's a several step process.
- 02:11 So that's what we're going to look at now.
- 02:12 So first things first,
- 02:13 we have to create a new column with whatever we want to change these into.
- 02:17 So we've learned in the past how to add new columns, we just go my_df, and
- 02:23 then name the column that we want to add, so let's just call this New, I guess.
- 02:28 And we can set this equal to anything we want.
- 02:30 Let's just create a little list here.
- 02:32 And instead of A, B, C, D, maybe we want to change this to
- 02:38 E, F, G, and H.
- 02:43 So we can add this column.
- 02:46 Now, if we run my_df, we see, hey, there's our new column, it showed right up,
- 02:51 just as we expect it to.
- 02:53 Now we just need to tell our data frame, hey, use that column as the new index.
- 02:57 So to do that, we just set the index.
- 02:59 Just like we just reset the index, now we're going to set index.
- 03:03 So we just go my_df.set_index, and
- 03:06 then just pass in whatever new column you want to be the new index.
- 03:11 So we want this one to be New, it's just this New column right here.
- 03:15 We designate it right there, right?
- 03:18 So if we Shift+Enter to run this, now we have this New column and
- 03:22 our new indexes, E, F, G, and H.
- 03:25 And you'll notice the formatting changed a little bit here.
- 03:29 No big deal, it's just how this works.
- 03:32 These have been bobbed up just because this now has been added.
- 03:35 But now our index has been changed to those things.
- 03:38 And again, like we've done so many times,
- 03:40 if we run our new data frame, we can see this change wasn't actually permanent.
- 03:45 We would have to come up here and go inplace=True to make this permanent.
- 03:51 I don't really want to make this permanent, so
- 03:53 we'll just leave this the way it is.
- 03:55 So that's how you do that.
- 03:56 Now, just to refresh our memories, how do we drop this column?
- 03:59 Say we want to get rid of this new column completely.
- 04:03 We would just go my_df.drop and then pass in New.
- 04:11 But remember, we have to designate the axis.
- 04:13 Remember those pesky axes, however you would say that.
- 04:16 And if we want to make this permanent, inplace=True.
- 04:20 Now if we run this and then take a look at our data frame now, that new column is
- 04:24 gone, our index is gone back to the way it was, and all is right in the world.
- 04:29 So that's how to change your index or
- 04:31 how to reset your index back to its original numbers.
- 04:34 In the next video, we'll look at a multi-index.
Lesson notes are only available for subscribers.