In Polars, the DataFrame.slice() method is used to select a specific subset of rows from a DataFrame, similar to slicing a Python list or array. It takes an offset (the starting row index) and an optional length (how many rows to return), making it easy to extract a desired portion of the data.
In this article, I will explain the Polars DataFrame slice() method, covering its syntax, parameters, and usage. This function creates a new DataFrame by selecting rows based on a specified offset and an optional length.
Key Points –
slice()is used to select a subset of rows from a Polars DataFrame.- It takes two arguments:
offset(starting index) andlength(number of rows). - The method returns a new DataFrame and does not modify the original.
- Indexing in
slice()is zero-based, meaning the first row is at index 0. slice()can be chained with other DataFrame methods for efficient pipelines.slice()is consistent across both eager and lazy execution in Polars.- It's useful for pagination, sampling, or working with data windows.
- If the specified range exceeds the number of available rows, it returns as many as possible without error.
<a id="h-polars-dataframe-slice-introduction"></a>
Polars DataFrame slice() Introduction
Let’s know the syntax of the slice() function.
# Syntax of DataFrame.slice()
DataFrame.slice(offset: int, length: int | None = None) → DataFra
<a id="h-parameters-of-the-polars-dataframe-slice"></a>
Parameters of the Polars DataFrame slice()
Following are the parameters of the slice() method.
offset(int) - The starting index (row) from where to begin the slice.length(int | None, optional) -- If provided: Specifies how many rows to return.
- If None (the default): Returns all rows from the offset to the end of the DataFrame.
<a id="h-return-value"></a>
Return Value
This function returns a new DataFrame with only the sliced rows.
<a id="h-usage-of-polars-dataframe-slice-method"></a>
Usage of Polars DataFrame slice() Method
The DataFrame.slice() method in Polars is used to select a specific range of rows from a DataFrame based on their row positions.
To run some examples of the Polars DataFrame slice() function, let’s create a Polars DataFrame.
import polars as pl
technologies= {
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fees' :[22000,25000,23000,24000,26000],
'Duration':['30days','50days','35days', '40days','55days'],
'Discount':[1000,2300,1000,1200,2500]
}
df = pl.DataFrame(technologies)
print("Original DataFrame:\n", df)
Yields below output.
To extract the first 3 rows from a Polars DataFrame using the slice() method. The method you choose may vary based on the programming language you’re using.
# Using slice(0, 3)
df2 = df.slice(0, 3)
print("First 3 rows (using slice(0, 3)):\n", df2)
Here,
0: This is the offset, meaning start from row index 0 (the first row).3: This is the length, which means return the next 3 rows starting from that offset.
<a id="h-slice-rows-starting-from-index-2-length-2"></a>
Slice Rows Starting from Index 2, Length 2
To slice rows starting from index 2 with a length of 2 in Polars, you can use the slice() method.
# Slice from index 2, take 2 rows
df2 = df.slice(2, 2)
print("Sliced DataFrame (index 2, length 2):\n", df2)
Here,
2: start from the third row (indexing is zero-based)2: select 2 rows from there
<a id="h-slice-the-last-2-rows"></a>
Slice the Last 2 Rows
To slice the last 2 rows of a Polars DataFrame, you can use negative indexing with the slice() method.
# Slice the last 2 rows
df2 = df.slice(-2, 2)
print("Last 2 Rows:\n", df2)
# Output:
# Last 2 Rows:
shape: (2, 4)
┌─────────┬───────┬──────────┬──────────┐
│ Courses ┆ Fees ┆ Duration ┆ Discount │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str ┆ i64 │
╞═════════╪═══════╪══════════╪══════════╡
│ Python ┆ 24000 ┆ 40days ┆ 1200 │
│ Pandas ┆ 26000 ┆ 55days ┆ 2500 │
└─────────┴───────┴──────────┴──────────┘
Here,
-2: Start from the second-last row2: Take 2 rows from there
<a id="h-get-all-rows-from-index-2-to-the-end"></a>
Get All Rows from Index 2 to the End
To get all rows from index 2 to the end in a Polars DataFrame, you can use the slice() method.
# Get all rows from index 2 to the end
df2 = df.slice(2, df.height - 2)
print("Rows from index 2 to end:\n", df2)
# Output:
# Rows from index 2 to end:
# shape: (3, 4)
┌─────────┬───────┬──────────┬──────────┐
│ Courses ┆ Fees ┆ Duration ┆ Discount │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str ┆ i64 │
╞═════════╪═══════╪══════════╪══════════╡
│ Hadoop ┆ 23000 ┆ 35days ┆ 1000 │
│ Python ┆ 24000 ┆ 40days ┆ 1200 │
│ Pandas ┆ 26000 ┆ 55days ┆ 2500 │
└─────────┴───────┴──────────┴──────────┘
Here,
2is the starting offset (index).df.height - 2calculates the number of rows from index 2 to the end.
<a id="h-slice-entire-dataframe-offset-0-no-length"></a>
Slice Entire DataFrame (offset = 0, no length)
When you use the slice() method with only the offset parameter (and skip the length), Polars returns all rows from that offset to the end of the DataFrame.
# Slice entire DataFrame (offset = 0, no length)
df2 = df.slice(0)
print("Entire DataFrame:\n", df2)
# Output:
# Entire DataFrame:
# shape: (5, 4)
┌─────────┬───────┬──────────┬──────────┐
│ Courses ┆ Fees ┆ Duration ┆ Discount │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str ┆ i64 │
╞═════════╪═══════╪══════════╪══════════╡
│ Spark ┆ 22000 ┆ 30days ┆ 1000 │
│ PySpark ┆ 25000 ┆ 50days ┆ 2300 │
│ Hadoop ┆ 23000 ┆ 35days ┆ 1000 │
│ Python ┆ 24000 ┆ 40days ┆ 1200 │
│ Pandas ┆ 26000 ┆ 55days ┆ 2500 │
└─────────┴───────┴──────────┴──────────┘
Here,
offset = 0means start from the beginning.- If you omit the length, Polars will return rows from the offset to the end.
<a id="h-slice-with-offset-beyond-data-length-empty-dataframe"></a>
Slice with Offset Beyond Data Length (Empty DataFrame)
When you use df.slice() with an offset greater than or equal to the number of rows, Polars will return an empty DataFrame, but it will still have the same column names and data types.
# Suppose df has 5 rows
df2 = df.slice(10)
print("Sliced DataFrame with offset beyond length:\n", df2)
# Output:
# Sliced DataFrame with offset beyond length:
# shape: (0, 4)
┌─────────┬──────┬──────────┬──────────┐
│ Courses ┆ Fees ┆ Duration ┆ Discount │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str ┆ i64 │
╞═════════╪══════╪══════════╪══════════╡
└─────────┴──────┴──────────┴──────────┘
Here,
- Tries to start slicing from row index 10
- But the DataFrame only has 5 rows (index 0 to 4)
- So, the result is an empty DataFrame
<a id="h-conclusion"></a>
Conclusion
In conclusion, the slice() function in Polars provides a flexible way to retrieve specific portions of a DataFrame using an offset and optional length. Whether you're slicing just a few rows or starting from a specific index to the end, slice() makes it easy to control the subset of data you work with.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars DataFrame width Usage & Examples
- Polars DataFrame height – Explained by Examples
- Polars DataFrame clone() – Explained by Examples
- Polars DataFrame clear() Usage & Examples
- Polars DataFrame row() Usage & Examples
- Polars DataFrame sample() Method
- Polars DataFrame shift() Usage & Examples
- Polars DataFrame product() Usage with Examples
- Polars DataFrame fill_nan() Usage & Examples
- Polars DataFrame replace_column() – by Examples
<a id="h-references"></a>
