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.
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.
Here’s the syntax of the concat()
method:
concatenated_dataframe = pl.concat([Dataframe1, Dataframe2])
Dataframe1
and Dataframe2
: These are two-dimensional data structures representing data as a table with rows and columns.
Let's concatenate two DataFrames using the concat()
method. Here's the sample code in Python:
import polars as pl# DataFrame 1dataframe1 = {'Id': [10, 11, 12, 13, 14],'Item': ['Apple', 'Mango', 'Banana', 'Cherry', 'Peach']}D1 = pl.DataFrame(dataframe1)# DataFrame 2dataframe2 = {'Id': [15, 16, 17, 18, 19],'Item': ['Guava', 'Raspberry', 'Strawberry','Apricot', 'Orange']}D2 = pl.DataFrame(dataframe2)# Concatenate dataframes using concat() functionconcatenated_dataframe = pl.concat([D1, D2])print(concatenated_dataframe)
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:
How to scale and normalize data in Python using Polars
Learn how to transform raw data using Python's Polars library to scale it (0-1) and normalize it (mean 0, std 1).
What is DataFrame.clear function in Polars Python?
Learn how to use Polars' DataFrame.clear()
to create a null-filled copy, either empty if n=0
or with n
null rows.
How to reverse a DataFrame in Polars Python?
Learn how to use Polars, a Rust-based DataFrame library for Python, which offers a reverse()
function to efficiently revert DataFrame rows, providing an alternative to pandas.
How to rename the column names in Polars Python?
Learn how to use Polars' rename()
function to efficiently rename DataFrame columns using key-value pairs, enhancing data management and processing.
What is Polars library in Python?
Learn how Polars, a fast DataFrame library in Rust for Python, offers high-performance data manipulation and analysis similar to Pandas.
How to concatenate two Dataframes in Polars Python
Learn how Polars, leveraging Rust, offers efficient DataFrame concatenation in Python with the concat()
method.
How to perform a transpose of a Python Polars DataFrame
Learn how to use Polars' DataFrame.transpose()
to efficiently transpose DataFrames, with options for including headers and custom column names, enhancing data manipulation capabilities.
How to check the polars version in Python
Learn how to ensure the correct Polars version by using pip3 show polars
or by printing pl.__version__
in Python.
What is DataFrame.update function in Polars Python?
Learn how to use the update()
function in Polars to merge two DataFrames, updating the target with non-null values from the source, and supporting various join strategies.
Free Resources