How to perform the transpose of a Python Polars DataFrame
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.
The DataFrame.transpose() function
The DataFrame.transpose() function in polars facilitates performing transpose of the DataFrame by interchanging the rows with columns and vice versa.
Syntax
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 toTrue. 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 theinclude_headerparameter is set.column_names: It’s another optional parameter to add the column names for the transposed DataFrame.
Return value
The function returns a DataFrame that contains the transpose of the given DataFrame.
Code
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"]))
Explanation
Line 1: We import the
polarslibrary aspl.Lines 2–8: We define our DataFrame as
dffor 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 theinclude_headerset toTrueto include the header as a new column.Line 14: We compute and print the transpose with the
header_nameset toCategoryfor adding a column name for the header included as a column.Line 16: We compute and print the transpose with the
column_namesset toItem 1,Item 2,Item 3, andItem 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 ifn=0or withnnull rows.How to reverse a DataFrame in Polars Python?
Learn how to use Polars, a Rust-based DataFrame library for Python, which offers areverse()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 theconcat()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 usingpip3 show polarsor by printingpl.__version__in Python.What is DataFrame.update function in Polars Python?
Learn how to use theupdate()function in Polars to merge two DataFrames, updating the target with non-null values from the source, and supporting various join strategies.
Free Resources