Locked lesson.
About this lesson
We discuss how to select individual and multiple columns within a data frame.
Exercise files
Download this lesson’s related exercise files.
Selecting Columns and Multiple Columns.docx57.4 KB Selecting Columns and Multiple Columns - Solution.docx
55.4 KB
Quick reference
Selecting Columns and Multiple Columns
Selecting data from a column or specific columns is easy!
When to use
Use this whenever you need to select a column of data, or multiple columns.
Instructions
Given a dataframe named my_df with columns:
my_cols = ["Mon", "Tues", "Wed"]
To grab a specific column, such as 'Tues':
my_df["Tues"]
To Grab more than one column, pass a list of lists:
my_df[["Mon", "Wed"]]
Hints & tips
- Grab One Column: my_df["Tues"]
- Grab More Than One Column: my_df[["Mon", "Wed"]]
- 00:05 Okay, so we've got our data frame, it's looking really cool,
- 00:08 how do we grab some of these columns if we want to do something with them?
- 00:12 Well, it's actually pretty simple and that's what we're going to do now.
- 00:14 So let's just call my_df and then we can pass in whatever column we want to grab.
- 00:20 So if we want to grab Monday we just do it like this Shift+Enter to run and
- 00:25 you see here's Monday.
- 00:26 So, A is point 3381, there we go.
- 00:31 D is -0.292, 0.92 and we can just spot check.
- 00:38 Yeah, that looks the same, yeah, that looks the same.
- 00:40 So yeah, we've just grabbed this column.
- 00:42 Now you'll notice,
- 00:43 this looks an awful lot like a series that we looked at a couple of videos ago.
- 00:47 It's just got our data listed.
- 00:48 It's got our index values or rows, A, B, C and D.
- 00:53 And it says the names listed here is Monday because that's the column head and
- 00:58 the data type is a float 64 bit.
- 01:00 It doesn't mention anything about a series so it may look like a series but
- 01:04 you may not be convinced.
- 01:05 If you're not convinced we can actually find out we can run a type function, and
- 01:09 then just pass in this whole thing and see what exactly this is.
- 01:14 And it's a panda's series.
- 01:16 So sure enough, it is a series.
- 01:18 So if you're curious about what our data frame is, we can run a type for
- 01:21 the data frame too, just grab our data frame right here has that in and
- 01:25 that's a panda's data frame which should have been obvious, I guess.
- 01:30 But very interesting, very cool.
- 01:31 So here, we're just grabbing one column, right?
- 01:36 And you may just want to do that, but you may want to grab more than one.
- 01:39 So how do we do that?
- 01:40 Well, we can just call my_df again.
- 01:43 And earlier we passed in a list, right?
- 01:46 Now we want to pass in a list of lists, so double brackets, and
- 01:50 then just inside of here, just designate which ones you want.
- 01:55 Say we want Monday and Wednesday.
- 01:56 If we Shift+Enter to run this, boom, we get Monday and Wednesday.
- 02:00 Notice Tuesday is gone.
- 02:02 Now notice these are case sensitive.
- 02:06 So if we wanted to call lowercase monday, we're going to get an error.
- 02:11 So we get this key error,
- 02:12 if we scroll down, usually when you get an error, and you're going to get errors all
- 02:16 the time cause you're going to have typos and things.
- 02:18 But you want to kind of look down towards the bottom and it usually tells you at
- 02:23 very last thing is what exactly is going on, since Monday is not an index.
- 02:27 Well, that's because we misspelled Monday.
- 02:29 Because these guys are case sensitive, it has to be uppercase or lowercase.
- 02:35 If it's in fact, uppercase or lowercase, very cool.
- 02:38 Those are columns, that's how we grab them and
- 02:42 we can do different things with them once we've grabbed them.
- 02:44 We'll look at that going forward.
- 02:46 But yeah, pretty much all there is to columns.
- 02:48 In the next video, I'll show you how to create new columns
- 02:51 once your dataframe is already been created.
- 02:53 If you want to add, for instance, a Thursday column on there,
- 02:56 we'll look at how to do that in the next video.
Lesson notes are only available for subscribers.