00:00 In this lesson, you’ll be renaming some of the headers of the Olympic dataset. As they stand currently, the names aren’t very good. One of the first things you’ll probably want to do before looking into even slicing the data and having a bit of a look at how it’s structured is to rename the headers and tidy up the column names and the labels of the columns because right now they’re being automatically assigned by pandas.
00:27 As you can see here, the labels given are just numbers, likewise with the rows. So you want to first make this the header, and then you’ll want to rename these because at the moment they are a bit messy.
The way you can approach this is by using the
.rename() method. There’s a couple ways to use this. You can use a dictionary as a map for renaming. That is, on one hand, you have your keys, which are the actual column names.
01:07 For example, if your column names change, for whatever reason, you may want to suppress any errors or not. The thing you want to do is to have the errors raise up, so you know, something has changed.
And you’re also going take this opportunity to understand what the
inplace argument means and why you generally shouldn’t use it. It’s present in a lot of methods in pandas, and essentially it means you change the DataFrame in place, meaning that you’re not sort of creating a copy of it, which is what a lot of methods do. They will take the original DataFrame, transform it with something like the
.rename() method and then return a new DataFrame.
Back in VS Code, you’ve got your code that will read the CSV Olympics and will return a
DataFrame with that data. A handy thing about looking at your code through this interactive window and using Jupyter and IPython in general is you can take your
DataFrame, get a method, and we’re looking at the
.rename(), and instead of calling it, you can append a question mark (
?) here, and now you can press Shift + Enter or Control + Enter, or this play button (▷).
It will output the documentation of the
.rename() method. As you can see, first it’s telling you that the output of this exceeds the size limit, and you can output the full data in a text editor. So if you click this,
02:42 you’ll get a messy output because a lot of these are ANSI control codes, which tell the console to color things and highlight them. But in a plain text file, this obviously does not work, but here you can see it says it takes a mapper, which is the dictionary, and it takes a bunch of different arguments.
And here it gives you a bit about the function:
dict values must be unique.
Labels not contained in a dict / Series will be left as-is. Extra labels listed
don't throw an error. Things like this.
It’s just a very useful shortcut to the documentation if you just want to consult something quickly and not have to go to the website. The
read_csv() function has an argument you can add in called
And the argument you’ll use with
.rename() is the
columns keyword argument. Now to this, you can pass in a dictionary which represents a one-to-one mapping of the existing headers and the ones that you want.
So the first one is called
Unnamed: 0. So copy this, have it as your first key—you’ll need to wrap it in speech brackets (
"")—and have a look at what the data is here. Now, this looks like they are countries, so how about calling this column
Save, Control + Enter to run, go down here, have a
SyntaxError because there’s no comma separating the key-value pairs. Shift + Enter again to run. Down here, this is run. Up (↑), Control + Enter to run this again, and now you can see that
winter_olympics has been renamed here.
06:11 There are many conventions, but one of the conventions is just to use typical Python snake case here for all the column names, which just makes it a bit more intuitive to get the names and reference things. This script is looking good. It cleans up all the headers, puts the header in the right place, and renames them all into something a bit more usable. Running this,
it would seem to be all good. So this dataset could now be considered clean enough to start some data analysis on it. In the next lesson, you’re going to be reviewing
.loc, sometimes called LOC or the location indexer, for slicing and dicing and exploring your data.
Become a Member to join the conversation.