Polars is a powerful library for fast and efficient data manipulation. It’s designed to provide high-performance operations on large datasets and handles them more quickly than the pandas library.
Note: Learn more about the difference between the Polars and pandas library here.
DataFrame.transpose()
functionThe DataFrame.transpose()
function in polars facilitates performing transpose of the DataFrame by interchanging the rows with columns and vice versa.
The syntax for the transpose function is given by:
DataFrame.transpose(<include_header>, <header_name>, <column_names>)
include_header
: It’s a boolean parameter that will add the column names as the first column in the transposed DataFrame if its value is set to True
. It’s an optional parameter.
header_name
: It’s also an optional parameter that is used to specify the name for the column added when the include_header
parameter is set.
column_names
: It’s another optional parameter to add the column names for the transposed DataFrame.
The function returns a DataFrame that contains the transpose of the given DataFrame.
import polars as pldf = pl.DataFrame({"Product": ["Note holder", "Scissors", "Stapler", "Paper clip"],"Price": [2, 1, 24, 25],"Quantity": [70, None, 30, 200],})# Computing the transposeprint(df.transpose())# Computing the transpose with the header included as a new columnprint(df.transpose(include_header=True))# Computing the transpose with the header included and the column name specified for itprint(df.transpose(include_header=True, header_name="Category"))# Computing the transpose with column namesprint(df.transpose(column_names=["Item 1", "Item 2", "Item 3", "Item 4"]))
Line 1: We import the polars
library as pl
.
Lines 2–8: We define our DataFrame as df
for the stationary shop with the product’s name, price, and quantity columns.
Line 10: We compute and print the transpose of the DataFrame using the df.transpose()
function.
Line 12: We compute and print the transpose of the DataFrame using the df.transpose()
function with the include_header
set to True
to include the header as a new column.
Line 14: We compute and print the transpose with the header_name
set to Category
for adding a column name for the header included as a column.
Line 16: We compute and print the transpose with the column_names
set to Item 1
, Item 2
, Item 3
, and Item 4
.
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