Hello everyone and welcome to the lecture.
Remerging joining cannonading data frames this lecture.
We're going to learn how to combine data frames through a variety of methods instead of actually life
coding everything out since these methods and commands are actually really simple and most of the typing
is just actually creating data frames to merge or join together.
We're going to do is just walk through the lecture notebook for this lecture.
It's called merging joinery and concatenating and it's under the panda's folder.
Let's go ahead and jump to that Jupiter notebook now.
All right.
Here we are at the Jupiter notebook for merging joining and concatenating.
First off we're going to create some example data frames to work with here.
I've imported pandas PD and using a dictionary and a list of index numbers.
I've created three data frames one DFA to ADF three we take a look at these data frames will see that
we have diff one which is columns A B C and D and indexes 0 through 3 and then DF 2 has the same columns
that the indexes are 4 through 7 and then DF 3 has the indices through 11.
Let's talk about concatenation concatenation basically glues together the data frames.
Keep in mind that the dimensions should match Machel on the axis.
You're trying to concatenate on for this.
You just use PD dot com cat and then passen a list of the data frame's to concatenate together here
I'm going to concatenate data frames one two and three by passing them into a list into the PD.
Concat function.
We can see that by default the access to join on is zero.
Meaning it's going to join the rows together.
We see here that successfully concatenated or basically glued together the data frames and we can see
now we have a b and c and d as columns with the indices 0 all the way up to 11.
With each of the data points lining up nicely.
Now you can also specify x is equal to 1 if you want it to join and merge along the columns or concatenate
along the columns.
And this is an example of what happens when you use PD that Kancha on these three data frames along
.
X is equal to 1.
Notice here we have a bunch of missing values and that's because these data frames didn't have values
for all the indices that you wanted to concatenate on.
For example with data frame 1 A B and C and D you had the values for 0 through 3.
However for all the way up to 11 you had no values and that's why it filled in with Knolles when you
joined this are concatenated on data into and data frame 3 which basically means when you're working
with concatenation and P-doc concat you'll want to make sure that you have information lining up correctly
when you join on the axes.
In this case we wanted to join an access equal 0 to line everything up correctly.
How do we specify the join along the columns.
Fax a sequel to one.
We'll have a bunch of missing information.
Most often you'll probably be joining columns together so you'll in fact be joining along.
Access is equal to 1.
However make sure you know your data and what you actually want to concatenate before choosing an access
.
All right let's go ahead and move along.
We'll create some more example data frames in this case.
One called left and one called right here for Take a look at left.
We have columns A and B with some values in them and then we have a key column k 0 through K3 we have
a right data frame which has a very similar data frame except it has different columns C and D.
However the key column is exactly the same as the left so heavily left data frame and the right data
frame panderers is capable of using the merge function to do merge logic.
That's very similar to merging sequel tables together.
And if you don't work of sequel you don't have to worry too much about this but I'll show you the basics
of how to merge two data frames.
You just say PD that merge and then passen the left data frame and the right data frame.
Or just pasan data from one or data frame to whatever data frames you want to merge together.
By default it will merge them on inner.
And if you're unfamiliar with inor as regards to siecle merging don't worry about it just use the default
inner.
More importantly learn about this on.
Basically when you're merging you're going to want to merge on a key column and you can actually pass
in one or more key columns which we'll see later.
But basically what's happening here is we're going to combine or merge these data frames together on
this key column.
And that way instead of concatenating them we would have two key columns we can merge so that we merge
them or join them on the same key column that they share.
So the logic here is when you're using the PD that merge instead of just gluing the data frames together
you're going to see where the values match up on columns.
In this case the key column and then create the rows using that key column.
Let me go and show you a couple of more complicated examples.
Here I have left and right data frames and will notice here that they have two keys one and key two
otherwise they're exactly the same as the left and right we just saw earlier you can use PD that merge
just pass in the first data frame and the second data frame and then you can specify on and you can
pass in a list of keys if you want to actually merge on multiple keys such as key one or key to.
And here are some more examples using merge outer or merge.
Right now if you're not familiar of sequence syntax such as inner or outer right or left joins the worry
about this too much we don't actually use these in the course we will always default to something that
looks like this in the course we just use an inner join.
However if you find yourself trying to do the sort of sequel merging and you're already familiar of
sequel syntax just know that Pancho's has this how operator are parameter for you to use in case you
want the extra functionality.
But basically depending if you are using outer writes inner etc. You may find that you have some missing
values so keep that in mind as you're working with PD the merge.
Here's an example of left.
And finally we've arrived at joining joining is a convenient method for combining the columns of two
potentially differently index data frames into a single resulting data frame.
You could essentially think of this as the same thing as merge except the keys you want to join on are
actually on your index instead of a column.
So here I've created two data frames left and right.
Notice they have columns A B and then CD and then the index is actually the is we're going to join them
and the syntax you can use is say left join right in will automatically do an inner join between the
left and the right.
Based off of the index keys if you wanted to merge these two data frames based off of one of their columns
you'd have to use merge but the join method functionality comes off of one of the data frames.
You say that join and then pass on the other data frame and just like merge.
You can specify HOW such as inner left outer etc..
Remember this only for event sequel users who are already familiar with that terminology.
If you're unfamiliar with that don't worry about it too much.
Let's go ahead and do a quick review of how to join merge and cottony data frames and we'll go over
the key topics you should get out of this lecture.
As far as merging joining and concatenating data frames in order to concatenate or Glew data frames
together the process is quite simple.
You just say pecan cat and you pasan a list of your data frames and then you make sure to specify the
correct axis depending if you want to join them together by the rows or by their columns.
Next you have PD that merge and PD that merge allows you to merge data frames together and you just
passen your first data frame you want to merge and then your second data frame you want to merge and
then you say on and you can Pessin just a single column key.
Or you can pass any list of keys and by default it will merge on enter.
If you're unfamiliar with merging sequel tables don't worry about this how to much just worry about
one data frame on the left one data frame on the right and then on a certain key column or on a list
of key columns.
That's really the only functionality of PD merged I will be using throughout the course and then for
joining is a more advanced method for combining columns of two potentially differently indexed data
frames.
It's very similar to merge except they are actually joining on the index instead of a column and the
syntax for that is just the name of the Left data frame drawing on the right.
We actually won't be using join throughout the course but I want you to be aware that and you can reference
this notebook book or the documentation in case you ever want to review joining.
All right great job.
Thanks and I'll see you at the next lecture