- 18 minutes ago
Combine separate datasets with confidence by learning when to merge, join, or concatenate DataFrames in Python Pandas. Using two Lord of the Rings datasets as examples, this beginner-friendly tutorial shows how to match records by Fellowship ID, choose inner, outer, left, right, and cross joins, and understand missing values and overlapping columns. It also explains how Pandas join works with indexes and why concat stacks or aligns data rather than matching records in the same way.
Explore practical Pandas DataFrame merging with clear examples of `merge()`, `join()`, and `concat()`—essential skills for combining CSV files and preparing data for analysis. A useful Python data analysis tutorial for beginners learning database-style joins, DataFrame operations, and how to combine datasets in Pandas.
Explore practical Pandas DataFrame merging with clear examples of `merge()`, `join()`, and `concat()`—essential skills for combining CSV files and preparing data for analysis. A useful Python data analysis tutorial for beginners learning database-style joins, DataFrame operations, and how to combine datasets in Pandas.
Category
📚
LearningTranscript
00:00Hello, everybody. Today we're going to be talking about merging, joining, and concatenating data
00:04frames in pandas. This whole video is basically around being able to combine two separate data
00:09frames together into one data frame. These are really important to understand when we're actually
00:13using the merge and the join. Right here we have what's called an inner join, and the shaded part
00:19is what's going to be returned. It's only the things that are in both the left and the right
00:24data frames. Then we have an outer join or a full outer join, and this will take all the data
00:30from
00:30the left data frame and the right data frame and everything that is similar. So basically it just
00:35takes everything. We also have a left join, which is going to take everything from the left, and then
00:40if there's anything that's similar, it'll also include that. And then the exact opposite of that
00:44is the right join, which is going to give us everything from the right data frame, and it's
00:49going to give us everything that is similar, but it's not going to give us anything that is just
00:52unique to the left data frame. So this is just for reference because in a little bit when we start
00:57merging these, these become very important. So I just wanted to kind of show you how that works
01:01visually. So let's get started by pulling in our files. So first we're going to say import
01:07and as as pd. We'll run this, and then we'll say data frame one, and we'll also have a data
01:13frame two,
01:14and these are the different data frames, the left and the right data frame, that we'll be using to join,
01:19merge and concatenate. So we'll say data frame one is equal to pd.csv underscore read, and we'll do
01:27r, and here is our file path. So we have this lotr.csv, that's our Lord of the Rings csv,
01:34and let's call that really quickly so we can see what's in there. And I'm having a dyslexic moment
01:40because it's supposed to be read underscore csv. I apologize for that, but this is our data frame.
01:46This is our data frame one. We have three columns. It's their fellowship ID, 1001, 2, 3, and 4,
01:52their first name, Frodo, Samwise, Gandalf, and Pippin, and their skills, hide and gardening,
01:56spells, and fireworks. So this is our very first data frame that we're going to be working with.
02:01Let's go down a little bit. Let's pull this down here, and we're just going to say data frame two,
02:08data frame two, and this is the Lord of the Rings two. So let's pull this one in now.
02:13As you can see, it's very similar. We have fellowship ID, 1, 2, 6, 7, 8. So we have three
02:20different IDs here. We don't have 6, 7, and 8 in this upper, this first data frame. We also have
02:26the first name. So Frodo and Sam, or Samwise, are in the very first and the second data frame.
02:32But now we have three new people, Baromir, Elrond, and Legolas. And now we have this age column,
02:38which again is unique to just this second data frame. Really quickly, I want to give a huge
02:42shout out to the sponsor of this video, and that is Zendesk. I've been using Zendesk for my company's
02:46customer analytics, and it has been absolutely phenomenal. They're going to be hosting a
02:49conference called Zendesk Relate on May 10th, and they're going to talk all about customer analytics,
02:54chatbots, and AI in this space. You can attend in person in San Francisco, or you can attend
02:58virtually, but space is limited. So be sure to apply if you want to attend. So if you are a
03:03business
03:03leader and you want to make the most out of your customer data, or you want to learn customer data
03:07analytics, I will leave links in the description. Again, huge shout out to Zendesk for sponsoring this
03:12video. Now, the first one that I want to look at is merge. And I want to look at merge
03:16first,
03:16because I think this one is the most important. I use this one more than any of the ones that
03:20we're
03:20going to talk about today. The merge is just like the joins that we were just looking at,
03:25the outer, the inner, the left, and the right. And there's also one called cross, and I'll show you
03:30that one. Although if I'm being honest, I don't really use that one that much, but it's worth showing
03:35just in case you come into a scenario where you do want to do that. So let's go right down
03:39here.
03:40And I want to be able to see these while we do it. So we're going to say data frame
03:44one. And when
03:45we specify data frame one as the very first data frame, we say data frame dot merge, this is
03:52automatically going to be our left data frame. Then if we do our parentheses right here, and we say
03:59data frame two, this is our right data frame. And let's see what happens when we do this.
04:05So what it's going to do in this, we didn't specify this. It's just a default. It's going to do
04:10an
04:10inner join. So it's only going to give us an output where specific values or the keys are the same.
04:16Now you can't see this, but what is happening is, is it's taking this fellowship ID and saying,
04:21I have 1001 here, a 1002 here. This is the exact same as up here with this fellowship ID and
04:28fellowship ID of 1001 and two. But when we look at 1003 and four, those aren't in this
04:34right data frame. And 678 is not in this left data frame. So the only ones that match are this
04:401001 and two. And that's why they get pulled in down here. But because we didn't explicitly say,
04:47here's what I want to join or merge between these two data frames, it actually is looking
04:52at the fellowship ID and the first name. So it's taking in these unique values of Frodo
04:57and Samwise, which are the same in both, which is why it pulled it over. But really quickly,
05:03let's just check and make sure that we did it on the inner join. Because again, we didn't specify
05:09anything that was just the default. So we're going to say how is equal to, and then we'll say inner.
05:15And if we run this, it's going to be the exact same because again, the inner is the default.
05:20But now just to show you how it's kind of joining these two data frames together,
05:26I'm going to say on is equal to, and then I'm only going to put fellowship ID. So let's run
05:33this.
05:33Now the first thing that you may have noticed is this first name underscore X and this first name
05:38underscore Y. What the merge does as kind of a default is when you are only joining on a
05:44fellowship ID, we have this right data frame with fellowship ID, the left data frame with the
05:48fellowship ID. If you're just joining on these and you're not joining on the first name and the first
05:54name, then it's going to separate those into an underscore X and an underscore Y. And even though
06:00they have the exact same values, since we are not merging on that column, it automatically separates
06:05that into two separate columns. So we can see the values within each of those columns.
06:10If we went into this on and we make a list and let's do it like that. And we say
06:16comma,
06:17and then we write first name, oops, first name. And then we run this. It's going to look exactly
06:25like it did before. Again, it automatically pulled in both of these columns when it was merging at the
06:31first time, even though we didn't write anything. But if we actually write this, it's doing exactly
06:35what it was doing when we just had DF two, we're just now writing it out. Now there are other
06:40arguments that we can pass into this merge function. Let's set shift tab. And let's scroll
06:45down here. So within this merge function, we have a lot of different arguments that you can pass into
06:50it. First, we have this right, which is the right data frame, which is data frame two, then we have
06:55the how and the on which we've already shown how to do. There's a left on right on left index,
07:01right
07:01index, not something you'll probably use that much. But you definitely can if you want to look into
07:07that. And there's all these doc strings will show you exactly how to use all of these. So if you're
07:11interested in looking at the left and the right and the left index, it's all in here. But one that
07:16is really good is the sort and you can sort it saying either it's false or true. Then we have
07:22these
07:22suffixes. Now if you remember, when we took these out, what it automatically did was it put in these
07:28underscore x and underscore y, you can customize that. And you can put in whatever you'd like
07:35instead of the underscore x and underscore y, you can put in some custom string for that. We also have
07:41an indicator and a validates again, all the things that you can go in here and look at, I'm just
07:45going
07:45to show you the stuff that I use the most. So these things right here are things that I definitely
07:50use
07:50the most. So now that we've looked at the inner join, let's copy this right down here. And let's look
07:56at the outer join. And these get a little bit more tricky. I think the inner join is probably the
08:01easiest
08:01one to understand. Let's look at the outer. It's spelled O-U-T-E-R. I don't know why I
08:08always want to say
08:09O-U-T-T-E-R. But let's run this and see what we get. So now this looks quite
08:14different. The inner join
08:17only gave us the values that are the exact same. This one is going to give us all of the
08:22values
08:23regardless of if they are the same. So we have one, two, three, four, six, seven, and eight. So let's
08:30scroll
08:30back up here. So we have one, two, three, four, one, two, and six, seven, and eight. So we don't
08:36have a
08:361005. And then if you notice in this data frame right here, if the value doesn't have, so if we
08:43can't join on the fellowship ID or the first name, like Legolas wasn't one that we joined on, or that
08:49has a similar value in the left data frame, it just gives us an N-A-N, which is not
08:54a number. And it's
08:55going to do that for any value where it couldn't find that join, or it couldn't match something within
09:00that either ID or first name. So in age, we also have that for the ones that weren't in the
09:06right
09:06data frame. We only had 1001 and 1002. So we'll have the age for both Frodo and Sam. But for
09:13Gandalf
09:14and Pippin, we don't have their corresponding IDs. And so it's just going to be blank for Gandalf and
09:20Pippin. And you can see that right here. So again, outer joins are kind of the opposite of inner joins.
09:26They're going to return everything from both. If there is overlapping data, it won't be duplicated.
09:32Now let's go on to the left join. And I'm going to pull this down right here. And now we're
09:37just
09:38going to say how is equal to left. And let's run this. So what this is going to do is
09:45it's going
09:45to take everything from the left table or the left data frame right here. So everything from data frame
09:50one. Then if there is any overlap, it'll also pull the overlapped or the, you know, whatever we're
09:56able to merge on from data frame two. So let's go back up to our data frame one and two.
10:01So it's
10:02going to pull everything from this left data frame because we're specifying we're doing a left join.
10:07So everything from the left data frame will be in there. We're also going to try to bring in
10:12everything from the right, but only if it matches or is able to merge. So just this information right
10:18here will come over. We weren't able to join on 1006, 1007, or 1008. So really none of that
10:26information is going to come over. So let's go down and check on this. So again, we have
10:31one, two, three, four, all of the data with this first name and skills, everything is in
10:36here. But then we are trying to bring over the age, but we only have matches with 1001
10:41and 1002. So only these two values will come in. Let's look at the right join because it's
10:47basically the exact opposite. Let's look at the right. And this is basically the exact opposite
10:54of the left in the fact that now we're only looking at the right hand. And then if there's
10:59something that matches in data frame one, then we will pull that in. So this is basically
11:04just looking like data frame two, except we're pulling in that skills column. And since only
11:101001 and 1002 are the same, that's why the skills values are here. Now, those are the main
11:16types of merges that I will use when I'm using a data frame or when I'm trying to merge a
11:21data
11:21frame. But there also is one called a cross or a cross join. And let's look at this one.
11:27And this one is quite a bit different. Here we go. Let's run this. So this one is different
11:34in that it takes each value from the left data frame and compares it to each value in
11:39the right data frame.
11:40So for Frodo in this left data frame, it looks at the Frodo in the right data frame,
11:46Samwise in the right data frame, Legolas, Elrond, and Baromir all in the right data frame.
11:51Then it goes to the next value, Samwise, and does the exact same thing. Frodo, Samwise,
11:56Legolas, Elrond, Baromir. And it does that for every single value.
12:00So let's go right back up here. So it's taking this, this 1001. It's comparing it to 1, 2, 3,
12:084, 5. Then it's taking Samwise. It's comparing it to 1, 2, 3, 4, 5. Gandalf, 1, 2, 3, 4,
12:145. Pippin.
12:14And then you kind of see that pattern. And that's what a cross join is. There are very few,
12:19in my opinion, reasons for a cross join. Although you'll, if you ever do like an interview where
12:24you're being interviewed on Python, you will sometimes be asked on cross joins, but there
12:29aren't a lot of instances in actual work where you really use or need a cross join.
12:35Now let's take a look at joins and joins are pretty similar to the merge function. And it can do
12:42a lot
12:42of the same thing, except in my opinion, the join function isn't as easily understood as the merge
12:48function. It's a little bit more complicated, but let's take a look and see how we can join together
12:54these data frames using the join function. So let's go right up here. We're going to say
12:58data frame 1.join, and then we'll do data frame 2. Very similar to how we did it before. And
13:05let's
13:05try running this. And it's not going to work. When we did the merge function, it had a lot of
13:11defaults
13:11for us. Let's go down and see what this error is. It says the columns overlap, but no suffix was
13:17specified. So it's telling us that it's trying to use the fellowship ID and the first name,
13:22just like the join did, except it's not able to distinguish which is which. And so we need to go
13:28in there and kind of help it out a little bit. Again, a little bit more hands-on than the
13:34merge,
13:34but let's see what we can do to make this work. Let's do comma and we'll say on. And let's
13:40really
13:40quickly, let's open this up and kind of see what we have. So this one has less options than the
13:45merge
13:45does. We have other, and that's our other data frame. We can do on, and we're going to specify,
13:50you know, what column do we want to join on? And then we can look at how do we want
13:54it to be a
13:55left and inner and outer, the same kind of types of joins as the merge. Then we have that left
14:00suffix, right suffix. And that's right here is kind of part of the issue that we were just facing is
14:06that those columns are the same. But if we say left suffix, it'll give us an underscore, whatever
14:12we want to specify any string for columns that are both in the left and the right, we can give
14:17it a
14:17unique name. So we'll no longer have that issue. And then we can also sort it like we did on
14:22the
14:22other one. But anyways, let's go back to our on. We'll say on is equal to, and then we'll say
14:29fellowship ID. Let's try running this and we're still getting an error. It's just not as simple
14:35as the merge. So let's keep going. So now let's specify the type. So we'll say how is equal to,
14:40and we'll do an outer. And if we run this, it still doesn't work. We're still getting the exact
14:46same issue as the left suffix and the right suffix. So now let's finally resolve it. I just
14:51wanted to show you how a little bit more frustrating it was. But now let's say L suffix is equal
14:58to,
14:58and now it automatically, when we did the merge, did an underscore X, but we can do,
15:02let's do underscore left. And then we can do a comma, we'll do right suffix. And we'll say is
15:11equal to, and we'll do underscore right. Now when we run this, it should work properly. Let's run this.
15:19So this is our output. And obviously it looks quite a bit different. Over here, we have this
15:25fellowship ID. Then we also have fellowship ID left, first name left, fellowship ID right,
15:31and first name right. So it just doesn't look right. Now, something I didn't specify when I
15:36first started this, because I kind of wanted to show you is that the join usually is better for
15:41when you're working with indexes. Before when we were using the merge, we were using the column names
15:47and that worked really well and is pretty easy to do. But as you can see right here, when we're
15:51trying
15:51to use these column names, it's not working exceptionally well. Let's go ahead and create our
15:57index. And then I can show you how this actually works and how it works a little bit better when
16:00we're working with just the index. Although you can get it to work just the same as the merge,
16:05it's just a lot more work. So let's go right down here and let's go and say df4. So we'll
16:11create a
16:11new data frame. We'll say df1.set underscore index. And we'll do an open parentheses. And we'll say we
16:20want to do this index on the fellowship ID. And then we're going to do the join. So now we're
16:28going to
16:28say join. So we're setting an index. So we're setting that index on the fellowship ID. Now we're
16:33going to join it on df2.set underscore index. And then we're also going to do that on the fellowship
16:41ID. And I'll just copy this. Oh, geez. I hate it when I do that. Okay. Now we also want
16:52to do and
16:53specify the left and the right index. So I'll just copy this because we do need to specify this.
16:59Now let's try running the data frame four. So really quickly, just to recap, we were setting
17:05the indexes. We were doing the same thing above, right? We have this join. We were joining data
17:09frame one with data frame two. Now we're joining data frame one with data frame two, except in both
17:15instances, we're setting the index as fellowship ID. So we're joining now on that index. So now let's run
17:22this. And this should look a lot more similar to the merge than the join that we did above,
17:28except now the fellowship ID right here is actually an index. So it's just a little bit different,
17:33but we can still go in here and do how is equal to outer. Oops, let's say outer. So we
17:41can still
17:42specify our different types of joins or the different way that we can merge or join these data frames
17:46together. We can still specify that. Again, it's just a little bit different. And that's why for
17:52most instances, I'm using that merge function because it's just a little bit more seamless,
17:56a little bit more intuitive. The join function can still get the job done. But as you can see,
18:01it takes a little bit more work. Now let's look at concatenate. Concatenating data frames can be
18:05really useful. And the distinction between a merge and join versus the concatenate is that the concatenate
18:11is kind of like putting one data frame on top of the other, rather than putting one data frame next
18:16to one another, which is like the merge and the join. So concatenating them is just a little bit
18:21different in how it'll operate. But let's actually write this out and see how this looks. Let's go up
18:26here and we'll say PD dot concat. We'll do an open parentheses. And then we're going to concatenate
18:33data frame one comma data frame two. That's all we have to write. And let's run this. And so just
18:40like
18:40I said, it literally took the first data frame one, two, three, four, and put it on top of the
18:46right
18:46data frame. One, two, six, seven, eight. So that is our left data frame. This is our right data frame.
18:52And they're literally just sitting one on top of the other. But just like when we merge either with
18:56a left or a right, when you have these skills and there aren't any values that populate for them,
19:01it is going to say not a number. And since we're not actually joining, we're not joining on one and
19:06two, even though this one and this one is the same rows, it's not populating that value because again,
19:12we're not joining these together. We're just concatenating and putting one on top of the
19:16other. Now, if we go into this concat, we say shift tab. There are a lot of different things
19:22that we can do, which if you remember the zero axis is the left hand index and the axis of
19:28one
19:28is the top index, which is the columns. So you can specify that. And we can also do joins. And
19:35this
19:35is the one that I'm going to take a look at, but there are other ones that you can look
19:38into as well.
19:39So let's look at join. Let's do comma and we'll say join is equal to, and let's do an inner
19:46join.
19:46So let's see what happens with this. As you can see, it is only taking the columns that are the
19:52same. That's what this inner is doing. It's joining these columns together. And the ones that were
19:57different, they didn't take because again, we weren't able to combine them. They aren't similar
20:02between both data frames. Let's do an outer. And now it's going to take all of them. And like I
20:08said,
20:08that's doing this on these columns right here, but we can also do it on this axis as well.
20:13So let's go ahead and say axis is equal to one. And when we run this, now it's joining us
20:19on this
20:20index right here of zero, one, two, three, four. So now these ones are being joined together and
20:25it's putting it side by side, much like a merge would. So that's how concatenate works. And I'm
20:31going to show you one more thing. And again, it's not up here in this title because it's not one
20:36that I
20:36recommend, but it's one called append. The append function is used to append rows from one data
20:41frame to the end of another data frame. And then we can return that new data frame. And so let's
20:46do
20:46data frame one dot append, do an open parentheses and we'll say data frame two, very similar to how
20:53we've been doing other things. And let's run this. And as you can see, this is almost exactly like how
20:58the concatenate did when we first did it. But if we read kind of this warning, it's saying the frame
21:03dot append method is deprecated and will be removed from pandas in the future version,
21:08use pandas dot concat instead. So it's literally warning us, you know, append is on its way out.
21:13If you want to do exactly what you're doing right here, go and try concat or concatenate because that'll
21:19do the exact same thing. So I'm not really going to show you any other variations of append because
21:23there's no reason it's going to be on its way out in the next version. So that is our video
21:29on merge,
21:29join and concatenate and append as well in pandas. And I hope that that was helpful. I hope that you
21:34learned something. I mean, this stuff is really important because oftentimes you're not just
21:38working with one CSV or one JSON or one text file, you're working with multiple of them and you need
21:43to combine them all into one data frame. And so this is a really, really important concept and thing to
21:49understand. With that being said, be sure to like and subscribe. Check out all my other videos
21:53on Python and pandas and I will see you in the next video.