Locked lesson.
About this lesson
There are different ways to combine various pieces of data within your data frame. In this video, we'll discuss concatenating.
Exercise files
Download this lesson’s related exercise files.
Concatenating, Merging, and Joining part 1.docx57.5 KB Concatenating, Merging, and Joining part 1 - Solution.docx
55.4 KB
Quick reference
Concatenating, Merging, and Joining part 1
Concatenation allows us to combine two or more DataFrames together.
When to use
Use the .concat() function whenver you want to combine two or more DataFrames.
Instructions
Given two DataFrames named df1 and df2, to concatenate them on rows:
pd.concat([df1, df2])
Given two DataFrames named df1 and df2, to concatenate them on columns:
pd.concat([df1, df2], axis=1)
Hints & tips
- Concat by Row: pd.concat([df1, df2])
- Concat by Column: pd.concat([df1, df2], axis=1)
- 00:03 Okay, in this video I want to talk about concatenating, merging, and joining.
- 00:09 So different ways we can combine different data frames together.
- 00:12 So let's say we have two or three data frames.
- 00:14 We want to combine them all into one data frame.
- 00:16 How do we do that? So
- 00:16 there's several different methods based on how we want to do it, all right.
- 00:21 And in this video, we're going to look at concatenating.
- 00:23 In the next couple of videos after this, we'll look at merging and joining.
- 00:26 So concatenating, and let me just type this out, concatenate.
- 00:30 It's kind of a weird word that some people haven't seen.
- 00:33 It just means kind of smushing together, right?
- 00:35 You're just combining two things.
- 00:36 And with concatenating, we're not doing any logic,
- 00:40 we're not merging these things based on the data that's inside of them.
- 00:44 We're just kind of slapping them together, right?
- 00:47 We don't really care.
- 00:47 So let me just paste in a couple of data frames here.
- 00:51 So we have df1 and df2, and we create them
- 00:55 the same way we've created data frames in the past with our pd.Dataframe function.
- 00:59 And inside here, I'm just going to use a Python dictionary because it's easier.
- 01:03 And so we have columns Monday, Tuesday.
- 01:06 And Wednesday, and Monday, Tuesday, and Wednesday.
- 01:09 So each of these data frames has the same columns.
- 01:11 And for the index, I just put a quick numbered index 1, 2, 3, 4.
- 01:15 For this one, this one's 5, 6, 7, 8.
- 01:17 So they have the same columns, but they have different indices, right?
- 01:20 So inside of here, for Mondays column we have M1, M2, M3, and M4.
- 01:26 For Tuesdays, we have T1, T2, T3, and T4.
- 01:29 For Wednesday, we have W1, W2, W3, and W4.
- 01:33 So I hope you see what I'm doing here.
- 01:34 I'm just creating columns of data that have the name of it.
- 01:38 W for Wednesday, T for Tuesday, and M for Monday.
- 01:42 For the second one, we're going to have the same format.
- 01:45 The same M for Monday, T for Tuesday, and
- 01:47 W for Wednesday, but the numbers are going to be different.
- 01:49 So these were 1, 2, 3, and 4.
- 01:51 These will all be 5, 6, 7, and 8, right?
- 01:54 And, of course, different indexes.
- 01:57 So let's go ahead and just run these and see what we've got here.
- 02:02 So we've got df1 and df2, Shift + Enter to run.
- 02:06 And so we see Monday, Tuesday, Wednesday.
- 02:07 Monday, Tuesday, Wednesday, they both have the same columns,
- 02:09 different indexes, right?
- 02:10 1, 2, 3, 4, 5, 6, 7, 8.
- 02:13 And the data is slightly different here at M1, M2, M3, M4, and
- 02:18 then M5, M6, M7, and M8.
- 02:21 So okay, just very basic data frames here with very basic data.
- 02:25 And so we just want to combine these two into one data frame.
- 02:30 How do we do it? Well, it's actually very easy.
- 02:32 We can just use the PD.concat function.
- 02:35 And inside of here we can just pass in a list of whatever data frames we want to
- 02:39 concatenate.
- 02:40 So we want df1 and df2.
- 02:43 When we run this, we get one big data frame.
- 02:46 And you'll notice we're sort of doing this on the row axis, the axis of 0.
- 02:55 Remember when we created columns way back when and we had to designate which axis
- 03:00 to put it on, the top ones here that was axis 1, the rows were axis 0.
- 03:04 By default, the concat function, combines on axis 0, which are these rows.
- 03:11 So we have 1, 2, 3, 4, 5, 6, 7, 8.
- 03:13 And the data is just these two data frames smush together.
- 03:18 So that's pretty cool.
- 03:20 It kind of makes sense to do these all on the axis because we have 1,
- 03:25 2, 3, 4, 5, 6, 7, 8.
- 03:27 It just seems to go together.
- 03:28 But if you wanted to do it by column, you could, you could just come up here and
- 03:32 designate the different axis.
- 03:34 So you can go axis = 1.
- 03:36 Remember, our columns are axis 1.
- 03:38 And look what we have here.
- 03:39 It's kind of interesting.
- 03:40 It's combining these two data frames, right?
- 03:43 But since we're doing it on this top axis,
- 03:47 the axis 1, each of these data frames has no data.
- 03:52 Because our first data frame doesn't have anything in row 5, 6, 7 and 8, right?
- 03:58 So we have no values for those.
- 04:01 The same thing with our second data frame.
- 04:05 It doesn't have anything for rows 1,
- 04:07 2, 3, and 4, so we get a bunch of nulls here, right?
- 04:11 So kind of an interesting thing to do it.
- 04:14 If you want to designate 0, you can and we just get the same thing we got earlier.
- 04:19 But the axis of 0 is the one by default.
- 04:22 So, you don't have to put that in, you could just run it like this.
- 04:24 So, that's concatenating.
- 04:26 Pretty simple.
- 04:27 It's just think of smushing them together.
- 04:28 I know smush is not a real word, but that's what I think of.
- 04:32 You're just combining two things, smushing them together.
- 04:35 We're not merging or joining based on any logic or anything like that.
- 04:39 We're just slapping these two things together.
- 04:42 And we use two data frames, you could use three, you could use 20,
- 04:45 it doesn't matter.
- 04:45 Any more than one is all you need to concatenate.
- 04:49 So that's cool.
- 04:51 So that's concatenating.
- 04:52 In the next video, we'll look at merging and joining.
Lesson notes are only available for subscribers.