Move from Excel to Python with Pandas Transcripts
Chapter: Intro to Pandas
Lecture: Working with column names
0:00 Okay, let's go through some more examples of how to work with Jupyter notebooks and
0:04 pandas, and I've opened up my notebook and I want to walk through something that
0:09 could be a little confusing to new users.
0:10 So if you look at this notebook,
0:12 I've just opened it up and you can see that in this cell I'm showing the
0:17 data frame by typing DF. And so there may be a temptation to go in
0:21 here and let's just take a look at the head and remember,
0:26 shift + enter. I press that and I get a name error df is not defined.
0:30 And the reason is I haven't actually run everything in the notebook,
0:34 so it's really useful to hit this menu option.
0:39 Kernel, restart and run all,
0:42 and you'll get this option to restart.
0:44 Run all cells. You do that and what this does.
0:47 It runs through all of the code from top to bottom and makes everything live in
0:51 the current Kernel. So now if I make a change,
0:54 everything works. You can also see that the number has incriminated.
0:58 So went from 1, 2, 4 5, 6, 7, 8, 9 and then back up to 10 and 3
1:06 is gone because I reran in that cell.
1:08 So this points to some of the power of Jupyter notebooks,
1:11 but also how it can be confusing sometimes if you get out of order.
1:15 So the thing I would recommend is that you frequently use Kernel Restart and run all
1:20 And if you don't want to use the menu,
1:24 this command here, restart the Kernel rerun,
1:27 everything will do the same thing.
1:29 So once we've done that, we've taken a look at our data frame.
1:33 And now we want to actually look at some columns.
1:37 So the simplest way to do this,
1:39 remember, we have. If you ever forget what columns do I have,
1:43 type df.head() and we have these columns called Invoice / Company / purchased_date.
1:48 So let's just say df.invoice and I see all of the invoice
1:55 column all of the values in the invoice.
1:58 You can see each one it truncates if you are in the middle because it doesn't want to
2:01 show 1000 rows, which makes sense.
2:03 It's pretty good.
2:06 That should be pretty intuitive to someone that has worked with python before.
2:10 But what happens if we want to look at this extended amount where there's a space
2:16 in the column name, you get a syntax error,
2:19 and that's because Python doesn't understand what this space means.
2:22 So the syntax you need to use is put a bracket around it and quotes,
2:31 and then you can reference the column and here you go,
2:34 so you can see that. 323, 420, 161, 203, 684.
2:35 if I scroll appear 323, 420, 161, 203, 684.
2:42 So the the reason I point this out is you have two options to access the
2:48 columns, and sometimes you'll see code that has that period versus the bracket notation.
2:55 I encourage you to always use the bracket notation.
2:58 It will make your life easier when you have these types of situations and it's consistent
3:02 with the other operations you're gonna want to do and pandas.
3:05 So the main reason I bring it up is so that you're aware of it,
3:07 and you can keep that in mind when you are doing your analysis and doing your problem solving.