filmov
tv
Rename Index Labels or Columns in a DataFrame - Data Analysis Tutorial

Показать описание
Rename Index Labels or Columns in a DataFrame is a free tutorial by Boris Paskhaver from Data Analysis course
Link to this course(Special Discount):
This is the best Data Analysis Course
Course summary:
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
Link to this course(Special Discount):
This is the best Data Analysis Course
Course summary:
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