Concatenating DataFrames
Concatenation in data means combining multiple assets, whether it be a table or DataFrame, into one. We also use it often as in concatenating strings. In the context of concatenating DataFrames, it means combining multiple DataFrames across rows or columns. Unlike the join operation, concatenation doesn’t require key columns.
In this recipe, we’ll look at how we can concatenate DataFrames in Python Polars.
Getting ready
We’ll be using the academic_df
DataFrame that we already created in the last recipe. Let’s split the DataFrame into a few DataFrames so that we can test the concat operation:
df1 = academic_df.head(2) df2 = academic_df.slice(2, 2) df3 = academic_df.tail(1) display(df1, df2, df3)
The preceding code will return the following output:
Figure 8.17 – The DataFrame split into two DataFrames
All of the DataFrames have the same number of columns. Only df1
and df2
have two...