Locked lesson.
About this lesson
Pandas series is one of the main "workhorses" of Pandas. We'll discuss how series work and some of the helpful ways you can use them.
Exercise files
Download this lesson’s related exercise files.
Pandas Series.docx57.1 KB Pandas Series - Solution.docx
56.7 KB
Quick reference
Pandas Series
Series are similar to Numpy Arrays, but they have an index.
When to use
Throughout the course we'll be mostly working with Pandas DataFrames, but DataFrames are made up of Series so it's important to understand them.
Instructions
A series contains data and labels. To create a Series:
label = ["Mon", "Tues", "Wed"]
data = [12, 41, 36]
my_series = pd.Series(data, label)
To pull specific data from a series...for Instance, Monday:
my_series["Mon"]
To create a series from a Python Dictionary:
dict = {"Mon": 12, "Tues":41, "Wed":36}
my_series = pd.Series(dict)
Hints & tips
- Series and DataFrames are the two main workhorses of Pandas
- DataFrames are made of Series
- 00:05 Okay, in this video I want to talk about Pandas series.
- 00:07 Now, there are two sort of main workhorses when it comes to Pandas.
- 00:11 The first one is a series and the second one is the data frame.
- 00:15 And we're going to be doing most of the work throughout this course with
- 00:18 the data frame.
- 00:19 But we need to talk about the series at least a little bit because data frames
- 00:22 are made up of series.
- 00:23 And they're just sort of important to understand.
- 00:25 So a series is very much like a NumPy array,
- 00:28 except that it has an index that we can label and play around with.
- 00:33 So a series are built off of NumPy arrays, so that's kind of important.
- 00:37 And I should notice up here we've still got our import numpy as np.
- 00:40 It's because Pandas uses NumPy for just a ton of stuff.
- 00:45 So anytime you want to use Pandas, yeah, you definitely have to also import NumPy.
- 00:49 So let's create one of these series real quick, and
- 00:53 let's create a Python list and call it label.
- 00:56 And let's just call this give us some labels of Monday as go Tuesday and
- 01:03 Wednesday, I'll just use abbreviations.
- 01:09 And then we can create some data, and let's just call this,
- 01:14 I don't know, 41, 32, and 16.
- 01:17 Now, we can create an array and let's go, let's just call it my_array.
- 01:23 And this is going to be a NumPy array, so np.array, and we can pass in that data.
- 01:30 I guess we could've put this in here, but I like to separate them out, just so
- 01:33 that we can understand exactly what's going on.
- 01:36 So all right, let's create a simple series.
- 01:38 So I'm going to call pd.Series.
- 01:42 Remember, right here we're calling pd, Panda as pd.
- 01:46 And then let's start out just by putting in our array and just see what this does.
- 01:54 So if we run this, Shift+Enter to run this, we see here is our series.
- 01:58 And here's our data 41, 32, and 16.
- 02:01 And over here, we have our index.
- 02:03 And there are no labels, it's just numbered.
- 02:06 And like all Python lists, they start at 0, 1, and 2, right?
- 02:10 So it always starts at 0.
- 02:11 So that's cool.
- 02:12 This is strictly speaking a series.
- 02:14 And you'll notice, it's different than a NumPy array because it has this index and
- 02:18 these labels.
- 02:18 But here we can actually designate what we want those labels to be.
- 02:21 So we can just slap a comma in here and type in the label.
- 02:25 Then we Shift+Enter, we get Monday, Tuesday, Wednesday.
- 02:28 Very cool and pretty easy to do.
- 02:30 So we can determine what this is by calling type.
- 02:35 And then we could just grab this whole thing here and paste it in, Shift+Enter.
- 02:42 Yes, this is in fact a series, that's kind of interesting to know.
- 02:45 So, okay, this is a series, right?
- 02:49 It's a data type of 32 integers of 32.
- 02:52 We could also, instead of putting numbers through here,
- 02:55 you could pass anything you want into a series, we could just do labels.
- 02:58 So we just put our label in there and shift around it.
- 03:01 Now we get Monday, Tuesday, and Wednesday as the data in our series and
- 03:06 we go back to having our numbered index.
- 03:10 So that's kind of fun.
- 03:13 Let's change this back.
- 03:15 So we can name this, we could just call this,
- 03:19 my_series, set that equal to this whole thing.
- 03:23 And now we can actually call things off of this so we can go my_series.
- 03:28 And then for instance if we just want to call the whatever was Monday, that was 41.
- 03:36 And we can designate this again by just calling my_series.
- 03:42 See, Monday 41, if we wanted Wednesday, we could just call it Wednesday.
- 03:46 That was 16, yep sure enough 16.
- 03:49 So very cool.
- 03:50 That's one way to create a series.
- 03:52 You can also create them using a dictionary.
- 03:54 So let's create a dictionary, just a basic Python dictionary.
- 03:58 And let's use our same data, we can go Monday, and then that was 41.
- 04:06 Tuesday and that was 32, and
- 04:10 then Wednesday and that was 16.
- 04:15 This just the basic Python dictionary but
- 04:19 now we can call pd.Series, and then just pass in that dictionary.
- 04:26 And we get the same thing.
- 04:27 And it will take the key value pairs and split them apart into our series.
- 04:33 And the keys, Monday, Tuesday and
- 04:34 Wednesday become the index labels in our series.
- 04:39 So those are series, like I said,
- 04:40 we're not going to use a lot of series stuff throughout this course.
- 04:43 We're going to be doing data frames, which we'll start to learn about in the next
- 04:47 video, but data frames are always made up of series.
- 04:49 A lot of times you'll break apart your data frame to do things.
- 04:52 And when you do that,
- 04:53 you'll break it apart into a series that you then pull specific data out.
- 04:59 So it's good to understand what they are and
- 05:01 at least have a basic knowledge of them.
- 05:03 And now we've got that.
- 05:04 So in the next video, we'll start to look at data frames.
Lesson notes are only available for subscribers.