Rename Index Labels or Columns in a DataFrame

Boris Paskhaver
A free video tutorial from Boris Paskhaver
Software Engineer | Consultant | Author
4.7 instructor rating • 6 courses • 297,684 students

Lecture description

In this lesson, we invoke the rename method on a DataFrame to change the names of the index labels or column names. We can either combine the mapper and axis parameters, or target the columns and index parameter exclusively. In either case, we provide an argument of a dictionary where the keys represent the current label names and the values represent the desired label names.

Learn more from the full course

Data Analysis with Pandas and Python

Analyze data quickly and easily with Python's powerful pandas library! All datasets included --- beginners welcome!

20:34:30 of on-demand video • Updated April 2021

  • Perform a multitude of data operations in Python's popular "pandas" library including grouping, pivoting, joining and more!
  • Learn hundreds of methods and attributes across numerous pandas objects
  • Possess a strong understanding of manipulating 1D, 2D, and 3D data sets
  • Resolve common issues in broken or incomplete data sets
English [Auto] In this lesson, we'll take a look at how we can rename one or more index labels or column labels in our data frame. So let's begin by executing our code to import our James Bond data set, set its film column, as are index labels and sort it as well. So the first method I want to introduce you to is called Name Makes Sense. Let's put our parentheses here and let's take a look at the documentation by pressing shift and enter. So there's actually two primary ways you can use this method. You can use the map or parameter in combination with the axis parameter, or you can use the index and columns parameters independently. And I want to show you both options. So as I mentioned, we can combine mapper and access to Axis. We specify which axis we want to target, either the row axis or the index labels right here, or the column axis, which means the column headers right here to map. Or we can pass a dictionary in that dictionary. The keys are going to represent the existing labels in that axis and the values for those dictionary keys are going to represent the new label that we'd like to set for that existing former label. That's a lot to take in. So let me show you an example. We're going to write out Mapper here. We're going to feed it a dictionary with curly braces. All right. So let's say I want to target the existing label Goldeneye. So I'm going to add Goldeneye as a key in my dictionary. And a dictionary, of course, consists of key value pairs. So I need to assign this key a value using the colon syntax. This is Vanilla Python. This is core pipe, not panners. And so the value for this key is going to be my new label. So let's say I want to put a space in between and Golden and I hear so I can set my new label to be gold in space I. If I want to overwrite multiple labels, well, then I simply provide multiple key value pairs to this dictionary that I pass to my map a parameter, for example, I can target the movie or label of the world is not enough. And I can overwrite that existing label with, let's say, a string of best Bond movies ever. That opinion gets me into a lot of trouble, by the way. Now, if this starts taking up a lot of space, what you can do is either assign your dictionary to a separate variable in the cell and then just provide it to Mapper. Or you can even break up your key value pairs one proline, which would make it a lot more readable. So by itself, this is going to work because by itself, by default pan, this is going to look for these keys on your row axis. So if I scroll down, we can see that golden eye has become golden eye and The World Is Not Enough has become the best Bond movie ever. This operation is not mutational, by the way. We're getting a brand new data frame right now. So if you want to be more explicit and specify that you're looking for these labels on your real axis, on your primary index right here, you could provide the axis parameter. A more explicit argument. For example, axis of zero means target the arrow axis. You can also provide a string argument to axis of rows, same result. Or you can provide a string argument to axis of index. So all four of these options that you see here in the cell will accomplish the exact same result. All right. They're going to overwrite these given row labels with these given labels and we're telling it where to look for it. So Mapper has to be combined with Axis. And the only reason it was working without it here is because it's defaulting to looking on the zero axis, which is the row axis, the horizontal one. Now, instead of using Mapper, what we can do is use a custom parameter here called index index is going to accept the exact same argument of a dictionary specifying the former and the latter the former label and the new label. But because we're specifying the index, we do not need to tilt the axis anymore. It already knows. In fact, if we combine the two, we're going to get an error here. It's going to say you cannot specify both axis and any of index or columns. So we don't need to tell it that we're on the axis of index because this parameter name tells us. So this is yet another way that we can accomplish the same thing. You can either combine mapper and axis or to explicitly write out the index parameter itself. I prefer this approach because it's one less. Now, as I mentioned, this is a non mutational operation. So if you want to make this permanent and overwrite the values in the bonded data frame, as always, there is our friendly in place equals true parameter we can provide. Of course, it's not going to return any output, but if I output bond below, we can see we have Goldeneye as well as best Bond movie ever. So let's go ahead and output our first row in the bond data frame just so we can be reminded of the column headers, and now I'll show you how we can rename the column labels instead. It's going to be the exact same logic. We can invoke the Renai method and we can do one of two things. We can either provide a dictionary to mapper and provide an axis specifying the columns, or we can provide that same dictionary to the columns parameter instead. Let's take a look at both options here. I'm going to provide a dictionary to Mapper and let's say I want to change my year column name right here. Let's say I want to change that to a string of release date. So here's my first key value pair and let's say I want to change the box office header to revenue. So I'm going to change box office to revenue. All right, by itself, this will not work because by default, Panas is going to look for these values in the index, it's not going to find them, so it's not going to change them. So here we can see here, still present and boxoffice still present. I need to tell pandas that I want to look for these existing labels of the year and box office in my vertical axis so I can pass my access parameter, either an argument of one here. Now, you can see that we've changed to release date and revenue as well right here. We can also pass a string of columns. And we're going to get the exact same result. And finally, our third option, just like we saw above, is to get rid of this axis parameter entirely and provide a dictionary not to the map or parameter, but instead to the columns parameter that tells us what to focus on. It tells pandas what key to look for and what value to overwrite it with, or alternatively, what current column name to look for and what new column name to write it with. And that is going to change those existing columns. Once again, this operation is not permanent. So if we want to make it permanent, we have to pass in that in place equals true parameter. And now if I take a look at my first row of the bond data frame, that's going to reveal the new column names and we can see that we've changed those two columns permanently. There's one more strategy to rename the columns that I want to show you in this lesson. It's particularly helpful when you want to rename all columns at once, but it can also be pretty useful if you want to rename a couple and want an alternate approach compared to the rename method. So you may recall that we have a columns attribute on our data frame that is going to give us the underlying index object, restoring the actual strings that make up the column headers. We can actually overwrite this attribute. It's a writable attribute on our object and we can overwrite it with a list of new string values. OK, so the length of this list has to be identical to the length of the original list. You have to provide a value for every column. So if you want to keep an existing column name, you're unfortunately going to have to type it in again. So, for example, if I want to change release date here, the first argument in my list can be the new string like year of release. But if I want to keep actor, I also have to write an actor. I'm going to proceed sequentially across all six columns in this case and provide the new name for that column. So if I want to keep actor and let's say I want to keep director, I'm going to have to write those values and let's say I want to change revenue to gross revenue and gross are both fourth in line or index position three. So I'm going to overwrite revenue to gross. Let's overwrite budget to cost and let's overwrite bond actor salary. Just salary. So in this one liner, the advantage is we don't have to provide the former keys or the former column names that we can just feed in the new list of six values sequentially. So it's going to take up a lot less space compared to passing a dictionary to the renamed method. However, if you want to keep existing column names, you are going to have to write them out manually. So when I execute this, we're not going to see any output. But if I output the first row to see the headers, you can see we've changed the column names right here. All right. That's all there is to cover in this lesson. We talked about the rename method, which we can use to change labels on either our index axis or our column axis. We can provide either a combination of the mapper and the axis parameter or use exclusively the index parameter or the columns parameter, depending on what it is that you want to target. All right. That's all there is to cover in this lesson. So I will see you in the next one.