How to concatenate two DataFrames in Polars Python

Polars, like pandas, utilizes a DataFrame-like structure to manage tabular data. However, Polars introduces its own DataFrame, which is built on Rust, a high-performance programming language. This design choice enables Polars to deliver impressive speed and memory efficiency.

Concatenating DataFrames

To concatenate two DataFrames in Polars (Python), we can use the concat() method. This method concatenates rows from one DataFrame to another, resulting in a new concatenated DataFrame.

Syntax

Here’s the syntax of the concat() method:

concatenated_dataframe = pl.concat([Dataframe1, Dataframe2])

Parameters

  • Dataframe1 and Dataframe2: These are two-dimensional data structures representing data as a table with rows and columns.

Example code

Let's concatenate two DataFrames using the concat() method. Here's the sample code in Python:

import polars as pl
# DataFrame 1
dataframe1 = {
'Id': [10, 11, 12, 13, 14],
'Item': ['Apple', 'Mango', 'Banana', 'Cherry', 'Peach']
}
D1 = pl.DataFrame(dataframe1)
# DataFrame 2
dataframe2 = {
'Id': [15, 16, 17, 18, 19],
'Item': ['Guava', 'Raspberry', 'Strawberry','Apricot', 'Orange']
}
D2 = pl.DataFrame(dataframe2)
# Concatenate dataframes using concat() function
concatenated_dataframe = pl.concat([D1, D2])
print(concatenated_dataframe)

Explanation

In the example code above:

  • Line 1: We import the required polars library.

  • Lines 4–8: We create the first DataFrame named D1.

  • Lines 11–15: We create the second DataFrame named D2.

  • Line 18: We concatenate the DataFrames D1 and D2 using the pl.concat() method and store the results in DataFrame concatenated_dataframe.

  • Line 19: We use the print() function to display the concatenated DataFrame.

Unlock your potential: Polars in Python series, all in one place!

To continue your exploration of Polars, check out our series of Answers below:

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved