Great again Pandas is a library that provides data structures
and data analysis tools in Python.
So in this lecture you'll see what I mean with data structures and data analysis so you'll see the main Pandas
objects which is a structure that contains the data and youll see thats how we get data from this structure and
analyze them.
So Ill open a simple command line. For now well use like Python which is great for data analysis
and working with data.
But even better is Jupiter notebook. Jupiter notebook is like a python shell.
It's actually a combination of a python shell and a python editor and it's a browser based tool where
you can write Python code and it's very efficient so it really boosts your productivity.
But for this lecture I want to start with simple things so one step at af time and Ill introduce
you to Pandas using iPython only and then in the next lecture I'll show you how to set up a Jupiter
Notebook previously known as the iPython Notebook.
So you'll set up Jupiter and I'll show you how to work with Jupiter.
So for now let's go ahead and use the plain iPython console. Import Pandas and normally the
first thing you do is you want to import some data.
Let's say from a CSV file or from a text file or even an excel file or JSON or other formats
which we'll be covering later.
But there are also other ways to create a Pandas data structure.
And the first thing you should know is that this data structure that I'm talking about is called a data
frame.
So that's a special object that will hold the data and you can create one. You can store a data frame
in a variable.
Let's say dF1 Pandas data frame.
So I'm creating a data frame manually passing values manually via Python.
Normally you want to use other files like I mentioned. So data frame and I now think of a bit of frame
Ill think of the data frame as a table.
So you may want to pass a list or lists where each list will be a row of that table.
So let's pass some data like 2 4 6 and 10 20 30. You execute that and that was successful because you didn't get
an error. DF1.
And here is the data frame.
So 2 4 6 is the first row 10 20 30 is the second row. This 0 1 2 are the names of the columns and these
are referred to as indexes.
So 0 1 here are indexes for the rows.
And these here are the column names.
The beauty of Pandas is that you can also have your own column names if you like.
So I'm going to call this expression again and I want to pass here a parameter called columns and that
expects a list of names which has to have the same number of items with a number of columns that
your data frame has.
So if you pass at least three items you want to pass three columns here as well.
Let's say price something random, age and value. You execute that and let me expand this DF1.
This time you see that you have your own column names there.
Now similarly you can pass custom names for indexes as well by passing in index ass parameter and then
youd want to pass at list with two items because we have two rows only.
Let's say first second DF1 and you have got some indexes.
However normally you won't have to pass costume indexes. Data normally have a defined number of columns
but through rows you may have hundreds or thousands of rows or millions there.
So really don't want to mess up with index names. However the feature is there in special cases.
So there's one way to create a Pandas data frame and you also have other ways of as well
which are not very common to use.
But I just you wanted to know that they are there.
So again you pass al list here and then what you could do is you would pass two dictionaries inside
that list so you can see the similarity.
Here we use a list of list.
Here we are also passing a list object but it's a list of dictionaries.
And I know what you could do here is you could pass the values of keys and values for a dictionary and
here as well.
So lets say a name, John.
Name
Jack.
Execute that. DF2.
And yeah.
This is yet another data frame.
If you wanted to have more columns there such as surname you would want to add here another key and
a value for the dictionary say something like
Surname.
And now if you execute that and DF2 youll get the surname column added there but for Jack you
get none because you didn't pass a surname in the dictionary.
In the second dictionary.
So if you do the same for Jack if you pass the surname you get the value in here.
So those are two basic ways to build data frames on the fly.
And as I said normally these values will come out of files of CSV files, Excel files etc.
I will do that throughout the next lectures and one more thing I wanted you to know.
So this is a this is a data structure.
And now what data analysis means is
from all of these data structures you want to get out information.
Sue you may want to extract for instance the average of all these values.
So 2 4 6 10 20 30.
So here we enter the face of data analysis and the approach to do that is you know these data frame object
now which you can see the type DF1.
So it's a data frame of Pandas.
Now this data frame object has methods attached to it.
If you do dir you see that it has quite a lot of methods that you can apply to it.
Let's locate the mean methods there.
Here its mean.
So what you could do is you point to DF1 and then mean and the brackets and you get the mean of all the
columns.
And if you one the mean of the entire data frame you can apply again
the mean method after that.
So what this does is it applies the mean method over these series here. So these type
It's a Pandas series object and series have more or less the same methods that you can apply to a data frame
.
So Df1 and DF1.Price and you get the series of the price column.
So that is also if you check with type.
That is also a Pandas series.
So a data frame is made of series and of course you can apply the mean method to Price as well.
Similarly you can apply other methods as well that are available there.
So max and you get the maximum value.
Yeah that's about the introduction to Pandas.
In the next lecture you'll learn about Jupiter.
It's very pleasant to work with Jupiter.
So don't miss that.
And then we will go have an open date frames, create frames out of files.
So I'll see you later.