Different DataFrame attributes in Polars

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.

Importing the library

First, let’s import the polars library:

import polars as pl
Importing polars library

Creating a DataFrame

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 pl
df = pl.DataFrame(
{
"Students": ["Joseph", "Danial", "Ema"],
"Calculus": [98, 85, 92],
"Data structures": [91, 87, 89],
"Operating system": [96, 88, 91],
}
)
print(df)

Attributes of the polars DataFrame library

Now, let’s look at some of the most common DataFrame attributes of the polars library, which are:

  • height

  • width

  • shape

  • dtypes

  • columns

The height attribute

The height attribute is used to get the count of rows in the DataFrame.

import polars as pl
df = pl.DataFrame(
{
"Students": ["Joseph", "Danial", "Ema"],
"Calculus": [98, 85, 92],
"Data structures": [91, 87, 89],
"Operating system": [96, 88, 91],
}
)
print(df.height)

The width attribute

The width attribute is used to get the count of columns in the DataFrame.

import polars as pl
df = pl.DataFrame(
{
"Students": ["Joseph", "Danial", "Ema"],
"Calculus": [98, 85, 92],
"Data structures": [91, 87, 89],
"Operating system": [96, 88, 91],
}
)
print(df.width)

The shape attribute

The 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 pl
df = pl.DataFrame(
{
"Students": ["Joseph", "Danial", "Ema"],
"Calculus": [98, 85, 92],
"Data structures": [91, 87, 89],
"Operating system": [96, 88, 91],
}
)
print(df.shape)

The dtypes attribute

The dtypes attribute is used to get the datatypes of the columns of the DataFrame.

import polars as pl
df = 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.

The columns attribute

The columns attribute is used to get the headers of the columns of the DataFrame.

import polars as pl
df = 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

Copyright ©2025 Educative, Inc. All rights reserved