- [Narrator] Next we will begin our introduction to the
pandas data structure called a DataFrame.
Which is basically an enhanced two-dimensional
array data structure.
Now, unlike two-dimensional lists,
or two-dimensional numpy arrays,
pandas DataFrames can have customized row
and column indices, as you're going to see.
They also offer a bunch of additional operations
that make them particularly useful for
data science orientated tasks and we'll begin
to scratch the surface of that here,
and continue that discussion in our
subsequent introductions to data science.
A key aspect of pandas DataFrames
is there support for missing data items.
And as you're going to see in a DataFrame
every column is itself a series data structure,
and those series can actually have different element types.
So it's not uncommon when you're looking at
a two-dimensional pandas DataFrames to have
many different types of columns within that DataFrame.
So let's go ahead and switch over to a terminal window
and let's begin taking a look at the concepts
of creating DataFrames and customizing their indices,
and we'll do this in several parts here as
we work our way through some of the DataFrames fundamentals.
So of course to begin we do need to import
the pandas library. So import pandas as pd
and just to save me typing time
lets go ahead and create a dictionary
from which we are going to create a pandas DataFrame.
So in this case we have key value pairs where the
keys are strings and the keys of course have to
be immutable values.
The values for each key are grades in this case.
So let's create that dictionary
and then let's turn it into a DataFrame object.
So DataFrame is the name of the class in pandas, excuse me,
and we're going to pass it, the grades_dict object
that we created up above and of course when
we then evaluate grades we can see the representation
of that dictionary converted into a pandas DataFrame.
So it's kind of interesting what happened here
the keys became the columns names and
the values within each columns are
each individual persons three grades
that were specified in these lists.
Now each of these lists has the same number of elements
however let's say that one of the list was
only two elements long, in that case there would be
a missing value when you compare that list
to all the other ones which had linked three.
So pandas would automatically put
a value into that element in the
corresponding column that represents no value
and we'll see examples like that
as we proceed through working with pandas DataFrames
in our intro to data science videos
and also in some of our higher end data
science and artificial intelligence case studies as well.
Now it is possible to also create a DataFrame
and give it a custom index.
So in this case we didn't give it an index,
so it just used zero, one, and two as the row indices.
And so what we could do is go ahead and
add an index to the existing grades array to
customize it or grades DataFrame rather, to customize it.
So we could do to that through the index attribute
of the DataFrame that we've already created.
Now the index corresponds to the rows in a DataFrame,
so if you were customizing the columns
there is a separate attribute that you can use
specifically for the columns, but the index
is always for the rows and the DataFrames.
And here what we're saying is rename
these indices from zero, one and two,
to test one, test two and test three.
Now we could have used an index keyword argument up here
and given it the lists of three values as well,
that also would have customized the index.
So after doing that we can see that we have
this nicely formatted grades DataFrame,
we see the names of our students across the top,
we see the names of the tests down the left,
and you also get this nice over all formatting
that's automatically created for you
when you get the stream representation of
a pandas DataFrame.
So if you think back to working with a
nested list structure to create a two-dimensional list
we would have to iterate on our own
through all the rows and columns,
and do all of our own formatting
to produce something like this.
But here we can simply evaluate a DataFrame
and it will automatically give us
this nicely formatted output.
Which makes it super convenient for working with
data that is structured into rows and columns.