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 • 283,210 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 September 2020

  • 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 re executing our code to import our James Bond data set set. It's film column as are index labels and sorted as well. So the first method I want to introduce you to is called Re 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 access 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 access 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 mapper 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 Python not panels. 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 golden and I here so I can set my new label to be golden 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 or 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 movie ever. That opinion gets me into a lot of trouble by the way. Now if this starts taking up a lot of space where 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 per line which 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 best Bond movie ever. This operation is nine mutations. 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 row 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 tables 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 0 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 tell if 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 just explicitly write out the index parameter itself. I prefer this approach because it's one less print. Now as I mentioned this is a non mutation operation. So if you want to make this permanent and overwrite the values in the bond 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 golden eye 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 rename 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 pandas 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 a year still present and box office still present. I need to tell pandas that I want to look for these existing labels of 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 while 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 access parameter entirely and provide a dictionary not to the map or parameter but instead to the columns parameter that tells pandas 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 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 is 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 a 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 rateable 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 3. 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 rename 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.