After meeting the Pandas Series data structure, it’s time to get more information about Pandas DataFrame data structure. You’ll learn how to build a Pandas DataFrame based on the Series you defined in the last video.
Working With Pandas DataFrames
00:00
Let’s talk about DataFrames next. So, let’s quickly write up a simple example here. Let’s just title our section, call it ## DataFrames
. Fantastic.
00:10
We will now then construct an example. We’re going to call it data1
, one object. It’s going to be a similar structure as what we had here above.
00:20
Maybe we will vary the numbering here, but it’s going to be something along those lines. We’re going to do 10
of those, just so we can get a… And this one will be, let’s say, between 0
and 10
. And then for the next one, what we’ll end up doing is something like this… data2
, it’s equal to something similar.
00:40
We will give it also 10
, but it’ll be between 11
and that number. So, we have two datasets that don’t overlap. Awesome. Now, how do you go about creating a DataFrame
?
00:54
A DataFrame()
takes a dictionary of objects, so we can create a DataFrame
like so. It can be JSON, they have all these convenience methods that allow you to consume all kinds of stuff. .to_csv()
, .from_csv()
, all types of stuff.
01:08
But if you give it a dictionary of lists, it’ll do the same thing. So that’s what we’re going to do, we’re going to call it 'data1'
here, copy all that stuff in data1
, it’s called 'data1'
. And the same thing here for 'data2'
.
01:28
And we now have a DataFrame
01:33
of 10 lists, 10 objects from 0 through 9, with 'data1'
consisting of numbers between 0 and 10 and 'data2'
with numbers consisting between 11 and 10,000. It’s kind of cool, you have the frame there. So, what type of things can you do with it? Well, you can do things like this.
01:50
You can add a new column, simply using the dictionary syntax that you’re hopefully mostly familiar with. You will then do something like this, so you’ll go to df['data1'] + df['data2']
,
02:06 and that’ll do a column-wise addition between the two columns and give you a new column with the resulting data. So then, once you call that,
02:17
you’ll need to print df
again, and then you’ll have a new column with the summation of the two things. So, as you can see here,
02:23 you added them together and all that. There’s much more possible with DataFrames, but that’s what I’m going to touch on today. Next, we’re going to move on to graphing some of this data while using Vincent.
Become a Member to join the conversation.