Hello everyone and welcome to the data frame's lecture and this lecture we're going to finally learn
about data frames building off of the serious object data frames.
It's going to be their main tool when working with pandas.
Let's go ahead and jump to the Jupiter notebook to see how it all works.
All right.
Here I am at the Jupiter Notebook.
We're going to go ahead and say important some pies and P and import pandas PD and I'm going to do one
last thing here to save from them Pied-Bot random import Rand and.
And then I'm going to go out and set a seed.
And what a seed means is just to make sure that we get the same random numbers.
That way if you're following me you'll see the same random numbers.
I see she can go in and say that random seed.
All right.
Let's go and explore the different command and the ADF is equal to PD data frame and you can actually
use tab here in Jupiter and up to autocomplete this.
And notice that the EFIS capitalize on Windu shift tab.
Check out the docstring and the initial signature for this PD data frame.
We have a data argument and an index argument just like we did for series but then we have this additional
columns arguments.
Let's go ahead and build this up with some random data and we'll see what a data frame actually looks
like.
First thing I'm going to do is pass in that data arguments and I'm actually going to call Ranz and 5
comma 4 and then the next ones you want to pass is the index in the index.
We can just make that a list of characters here.
I'm going to go in and say A B C D E.
And then finally for the columns I'm going to go ahead and say the x y z.
So I'll say w x y z going to go ahead and run this and then passen the data just just display and then
we get a nice display.
And this probably looks and feels a lot like Excel entries and a deeper notebook and that's good.
If you're using another ID editor you may not get such a nice crisp display but you should get a printed
output that looks similar to this.
All right.
So basically what we have here is a list of columns w x y z and corresponding rows A B C D E.
Now each of these columns is actually just a panda's series.
So W is a panda series as well as X Y and Z and they all share a common index and that's basically all
data frame is it's just a bunch of series that share an index.
Let's go ahead and show how we can use indexing and selection actually grab one of these serious objects
or several of these columns out one way and the main way to do this is by saying DMF and then using
Rackett notation and then passing in the column name.
In this case let's go ahead and grab the W column.
So as a string of pass w and this returns the W column and now take a look at the output.
Now this actually looks like a series.
And that's because that's exactly what W column is.
It's just a series and you can actually confirm this by saying.
Type of d f of W and it shows that it's Panas that core that series that series.
If I just say type on the DFA object itself I will get a data frame out.
So again just to reiterate that fact that a data frame is just a bunch of series that share the same
index.
Now there's two different ways you can grab a column from a data frame.
The main way and the way should always do it is using this sort of bracket notation and then passing
the column name.
However if you're really familiar of sequel a lot of times you're selecting a column you pass in a table
and then the column name.
This actually also works for pantless you could say dot and then the column name.
However I would recommend that you don't use this because it may get confused with the various methods
that are available off of the data frame.
For instance if I say DFA dot and then click tab I see a whole bunch of methods available for me.
And those are a bunch of methods that we're going to learn about as we learn more about pandas but what
may happen is that one of these methods gets overwritten by a column name and then Panther is going
to get confused whether you're asking for a method or an actual column name.
So you should do to avoid that as always is a sort of bracket notation when requesting a column.
OK.
If you want multiple columns back then you can go ahead and pasan a list of columns.
So know that I pass in my brackets and then I'm going to pass and a list of column names which is going
to result in another set of brackets here.
For instance if I want to columns W and Z I pass on a list here highlighted WNC and I get back a data
frame containing those two series.
And that means when I ask for multiple columns I'm actually getting back a data frame I asked for just
a single column that I will get back a series.
And in the Jupiter note book system that's the noted by the way the output is displayed.
OK let's go ahead and move on to creating a new column Palla's supports creation of new columns by just
specifying the column as if it already exists.
For instance new.
Now if I run this I'll get an error and it's a keeper.
The scroll all the way down here is say Kear new.
And it basically just is trying to tell you that it can't find it that there is no key new there.
However when you're creating a hash tag you let out and put this back in when you're creating a new
column.
You can actually define it as if it already exists and then on the right hand side of the equal sign
use other columns with arithmetic in order to pull off a new column for instance you can say DSF new
column as if it already exists is equal to DFW Plus the fly.
And this will run with no error.
And now if I check out DF if that new column is actually in place there and for removing columns you
can say DFT.
Drop you can say deific drop and pass in the actual column name you want to drop.
Now if you just do this by itself you'll get an error.
And let's go in and check out what that error is will say value error.
We scroll down.
It says labels new not contained in Access.
And if I click shift tab on the drop method notice that by default it says access is equal to zero access
equal to zero for the drop method refers to the index.
If I actually want to refer to the columns I need to specify access is equal to 1 and then it will drop
that new column here.
Now there's two things to note when using this DFT drop the first thing to know is that this doesn't
happen in place meaning I'm not actually affecting the state of frame unless I specifically specify
to this to occur in place.
I want to show you what I mean by that.
I've called this line D-flat drop you access equal to 1 if I called DMF I'll see that I still have that
new column.
That means I need to actually specify as an argument to shift have to show it the in-place argument
in many many Pandurs methods will require this in place arguments to be set to true.
And the reason panderers does that is that for you not to accidentally lose information in case you've
done a bunch of adjustments to your data you don't want to accidentally lose it.
So Pandurs requires you to say in-place equals true if you actually want a lot of these changes to stay
and occur in place.
And now if I call DFI running the cell notice that new column is now permanently removed because it
occurred in place and something to note here is that we can also use the left drop to drop rows for
instance.
Let's go out and use this to drop the e row.
I'll say DF got drop I pass an E and I can also say axis is equal to zero.
Now remember you actually don't have to add that in as an argument.
That's the default.
So let's go and just say the drop E and there we've been able to drop the e since Access is equal to
zero.
And it works either way I could say X sequel to 0 and this will work again.
And note here I'm not doing this in place.
OK.
Sometimes a point of confusion is why are the rows zero as far as are access and why are the columns
one where the reference actually comes back to pi.
Since data frames are essentially just fancy index markers on top of a PI array to show this.
I can say DF thought shape just like we did of a pipe matrix and no if that shape is a tuple for a tooth
imaginal matrix here and at the zero index or the number of rows.
So if I say ZF here I have five rows.
A B C and D E and then on the index one I have four columns w x y z.
Which is why rows are referred to as the zero axis and columns are referred to as the one axis because
it's directly taken from the shape just as you would have a pie array.
OK.
Now let's talk about selecting rows.
We've talked about selecting columns which you just do by saying DMF and in brackets you passen the
column name.
Or if you want many columns you pasan a list of the column names such as Z and then comma X and it close
off that bracket.
So I'm passing a list of column names and I return a data frame of those columns.
Let's talk about selecting rows.
Want to take a look at my data frame again here's my data frame.
ABC The E is my rose WXYZ are the columns.
There's two ways to select rows in a data frame and you have to call a method off of this.
And there's two methods to use.
One is Loek EHLO see for location.
And this is going to take in a label so you can also do shift tab here to see this property and something
to know here.
I'm using square brackets which is a little weird but that's the way it works with pandas.
You pass in LRC and then you passen in brackets the row you want.
For instance if I wanted the row I just passen.
See I passen the label of that index and this returns.
No a series.
And this comes to our second conclusion that not only are all the columns series but the rows are series
as well as far as the way they're going to get returns.
When you request them in Pandas.
Now let's discuss the second way to grab a row which is based off of next position.
I can use DFT.
I Loek to actually in a numerical index position even if my axes are labeled by strings.
For instance if I want see that row I can do that two ways I could either say DF ELO see for a location
and in brackets pass the row name which is C or I can say I EHLO see which is index based location and
counts what index it says.
So 0 1 2.
And then on I Alosi hasten to add this also returns that row.
And that basically means you have two ways of selecting rows using EHLO see if you want the location
or labeled based index or I ellos see if you want the numerical based index.
OK let's talk about selecting subsets of rows and columns and this will actually be very familiar to
you since we just learned about pi.
We can say DFI thought EHLO see if we want to select a subset of rows and columns we can pasan just
like we did with Num. Pi using common notation.
The row we want and then the column we want.
So I want the value at Row be column Y and this returns just a single value there.
So if I take a look at my DFA again looking at ráby column y I have this negative 0.8 for I get the
same result here.
So just like pi you can use this row comma column notation and if you want a subset of this you can
say Dhia ellos see and then pasan a list separated by commas.
For instance let's say I wanted the subset of a b rows with the W Y columns so basically I wanted this
2.7 0.9 and 0.6 5 and negatives are at twenty four.
That means I can just pass in a list of the rows I want which in this case is a and b.
And then I can pass in a list of the columns I want in this case that's w and y shift enter that and
I get the subset return of that data frame.
OK.
We're going to stop here for now and continue discussion.
In part two of the data frame's lecture and this is all in the notebook called Data frames under the
panderers folder.
Let's get a quick review of what we've learned as far as creating a data frame.
You just need to say PD data frame passenger data assin your index and then pass in your column names
.
Usually we won't be building data Frayne's by hand this manner will actually be reading data from some
sort of source like a CSP file but learn how to do data input now put later.
For now if you want to mainly create a data frame so you can do it then for selecting columns from a
data frame you can say DSF.
And then in brackets pass in the column you want or pass in a list of the columns you want remember
that each column is actually just a series.
You can create new columns based off of old columns by using this sort of arithmetic.
Then you can drop columns or rows using DFA that drop.
Remember the needs of say in place equals true.
If you want the effect to be permanent you say Axis 1 for columns and then Access 0 for rows.
And again that comes from this shape tuple 0 index was rows 1 index was columns as far as selecting
rows from a data frame.
There's two methods here.
There is Alosi method which is a labeled based index location or there's the dot.
I Alosi which is the integer based index location.
And then if you want to select subsets of the data frame you can use dot LOC and passen either values
or complete lists to return subsets of the data frame.
OK that's it for the very basics of data frames.
Coming up next we're going to discuss even more of what you can do if Appin the state of frame such
as using conditional selection.
All right thanks everyone.
I'll see you at the next lecture