Polars is a fast and efficient data manipulation library written in Rust. It’s designed to provide high-performance operations on large datasets and handles them more quickly than pandas. It’s particularly suitable when working with tabular data.
First, let’s import the polars
library:
import polars as pl
Let’s consider the example of a student’s score report. We create a DataFrame for the student’s scores in the following courses: calculus, data structures, and operating system.
import polars as pldf = pl.DataFrame({"Students": ["Joseph", "Danial", "Ema"],"Calculus": [98, 85, 92],"Data structures": [91, 87, 89],"Operating system": [96, 88, 91],})print(df)
polars
DataFrame libraryNow, let’s look at some of the most common DataFrame attributes of the polars
library, which are:
height
width
shape
dtypes
columns
height
attributeThe height
attribute is used to get the count of rows in the DataFrame.
import polars as pldf = pl.DataFrame({"Students": ["Joseph", "Danial", "Ema"],"Calculus": [98, 85, 92],"Data structures": [91, 87, 89],"Operating system": [96, 88, 91],})print(df.height)
width
attributeThe width
attribute is used to get the count of columns in the DataFrame.
import polars as pldf = pl.DataFrame({"Students": ["Joseph", "Danial", "Ema"],"Calculus": [98, 85, 92],"Data structures": [91, 87, 89],"Operating system": [96, 88, 91],})print(df.width)
shape
attributeThe shape
attribute is used to get the shape of the DataFrame. It returns a tuple of rows and columns that represents the dimensions of the DataFrame.
import polars as pldf = pl.DataFrame({"Students": ["Joseph", "Danial", "Ema"],"Calculus": [98, 85, 92],"Data structures": [91, 87, 89],"Operating system": [96, 88, 91],})print(df.shape)
dtypes
attributeThe dtypes
attribute is used to get the datatypes of the columns of the DataFrame.
import polars as pldf = pl.DataFrame({"Students": ["Joseph", "Danial", "Ema"],"Calculus": [98, 85, 92],"Data structures": [91, 87, 89],"Operating system": [96, 88, 91],})print(df.dtypes)
Note: We can also get the shape of the DataFrame and datatypes of the columns by printing the DataFrame.
columns
attributeThe columns
attribute is used to get the headers of the columns of the DataFrame.
import polars as pldf = pl.DataFrame({"Students": ["Joseph", "Danial", "Ema"],"Calculus": [98, 85, 92],"Data structures": [91, 87, 89],"Operating system": [96, 88, 91],})print(df.columns)
Free Resources