Concatenate Along the Row Axis
00:11 Before I will actually run this here in the lesson, I’m going to give you a blank screen and draw it out a bit. So remember, we have these two DataFrames that we’re working with here, and one is basically the fruits that consists of two columns, and then it has four rows. And then the second one is your vegetables, and that has a different shape: it’s got three rows and it’s got three columns, so it looks kind of like this.
Let’s look at that when you’re actually using
pd.concat() on these two example DataFrames. Okay, say
pd.concat(), and then for the function call, I need to pass a list—an iterable of two DataFrames, for example.
And when I run this concatenation, then you can see that you got exactly what we were expecting. You still have your
fruits DataFrame here, consists of two columns and four rows. And you have your vegetable DataFrame down here, just stuck below it, three columns and three rows.
So this gives you a quick overview of what
pd.concat() does without passing any other arguments. You’re just putting in these two DataFrames, and you’re getting your result down here. Now, the order matters.
What do you think is going happen when you do the same thing, but pass
veggies before you pass
fruits? You can pause for a second and try it out yourself, but probably your intuition is going to be right about this.
You get the same result, but in a different order. So now you can see you have the vegetable DataFrame up top, and then you have down here, the fruits DataFrame, and what is maybe a little bit surprising is the order where the
NaN values go, but that’s just because you have this specific order of the first DataFrame that gets preserved, and the vegetable DataFrame was first column
image. So this needs to be applied on the one that gets concatenated to it.
Become a Member to join the conversation.