In Polars, the head() function is used to retrieve the first n elements from a Series object. By default, it returns the first ten rows, but you can customize the number by specifying an integer as an argument. This method is particularly helpful when you want to inspect the beginning portion of a Series without needing to view the entire dataset, which is especially useful for large datasets.
In this article, I will explain the head() method in Polars Series, covering its syntax, parameters, usage, and how to returns a new Polars Series containing the first n elements from the original Series.
Key Points –
- The
head()method is used to return the firstnelements of a Polars Series. - It helps in quickly previewing the top portion of a Series.
- The method defaults to returning 10 elements if
nis not specified. - It accepts one optional parameter:
nof type int. - The result is always a new Polars Series, not a list or array.
- It does not modify the original Series, it’s a non-destructive operation.
- You can specify a custom number of elements using the
nparameter (e.g.,head(n=3)). head()is often paired withtail()to view both the beginning and end of data.
<a id="h-polars-series-head-introduction"></a>
Polars Series head() Introduction
Let’s know the syntax of the series head() method.
# Syntax of series head()
Series.head(n: int = 10) → Series
<a id="h-parameters-of-the-nbsp-polars-series-head"></a>
Parameters of the Polars Series head()
It allows only one parameter.
n(optional, int) - Number of elements to return from the beginning of the Series.Default is10.
<a id="h-return-value"></a>
Return Value
This function returns a new Polars Series containing the first n elements.
<a id="h-usage-of-polars-series-head-method"></a>
Usage of Polars Series head() Method
The head() method in a Polars Series is used to return the first few elements of the Series. By default, it returns the first 10 values, but you can specify any number using the n parameter.
Now, let’s create a Polars Series.
import polars as pl
# Sample Series
ser = pl.Series("values", [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60])
print("Original Series:\n", ser)
Yields below output.
When you use the head() method on a Polars Series without providing any arguments, it will return the first 10 elements by default.
# Default behavior of head()
print("Default head():\n", ser.head())
Here,
head()by default gives you the first 10 elements of the Series.- This is useful for quickly previewing your data, especially when working with large datasets.
<a id="h-using-series-head-to-get-the-first-3-elements"></a>
Using Series head() to Get the First 3 Elements
To retrieve the first 3 elements of a Polars Series using the head() method, you simply pass 3 as an argument to the method.
# Get first 3 elements using head()
print("First 3 elements:\n", ser.head(3))
# Get the first 3 elements
result = ser.head(3)
print("Last 3 elements:\n", result)
# Output:
# First 3 elements:
# shape: (3,)
# Series: 'values' [i64]
# [
# 5
# 10
# 15
#]
Here,
- The method
ser.head(3)retrieves the first 3 elements from the Series. - The result is a new Series containing the top 3 values from the original Series.
<a id="h-using-head-0-returns-an-empty-series"></a>
Using head(0) Returns an Empty Series
Using head(0) in Polars will return an empty Series. This is because the head() method retrieves the first n elements, and when n is set to 0, it essentially asks for zero elements, resulting in no data being returned.
print("head(0) result:\n", ser.head(0))
# Output:
# head(0) result:
# shape: (0,)
# Series: 'values' [i64]
# [
# ]
Here,
head(0)is a valid use case.- It returns a Series with zero elements, retaining the data type and name of the original Series.
- Useful in cases where you want to create a placeholder or initialize an empty result structure.
<a id="h-using-the-series-head-with-negative-numbers"></a>
Using the Series head() with Negative Numbers
If you pass a negative number to the head() method, it returns all elements except the last n, similar to slicing from the start up to -n. The head() method is designed to retrieve the first n elements of a Series, and it behaves the same way regardless of whether the Series contains positive or negative values. The method doesn't interpret the sign of the values; it simply returns the top n elements based on their position in the Series.
import polars as pl
# Sample Series with negative numbers
ser = pl.Series("values", [-5, -10, -15, -20, -25, -30, -35, -40, -45, -50, -55, -60])
# Get first 3 elements using head()
print("First 3 elements with negative numbers:\n", ser.head(3))
# Output:
# First 3 elements with negative numbers:
# shape: (3,)
# Series: 'values' [i64]
# [
# -5
# -10
# -15
# ]
Here,
- The
head(3)method works the same way as with positive numbers, and it retrieves the first 3 elements from the Series. - The negative signs don't affect the behavior of the
head()method itself. It simply pulls the firstnelements from the Series in their existing order.
<a id="h-using-head-on-a-string-series"></a>
Using head() on a String Series
When using head() on a String Series in Polars, the method behaves the same as it does with numeric Series. It allows you to retrieve the first n elements from the Series, where n specifies how many of the top elements you want to return. If you don't specify n, it defaults to 10.
import polars as pl
# Create a Series with string values
ser = pl.Series("fruits", ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"])
# Get the first 4 strings using head()
print("First 4 fruits:\n", ser.head(4))
# Output:
# First 4 fruits:
# shape: (4,)
# Series: 'fruits' [str]
# [
# "apple"
# "banana"
# "cherry"
# "date"
# ]
Here,
head()extracts the firstnstrings from the Series.- If the Series contains strings,
head()will return the firstnstring values in the same order. - The method is non-destructive, meaning it does not modify the original Series.
<a id="h-conclusion"></a>
Conclusion
In conclusion, the head() method in Polars is a straightforward but effective function for quickly viewing the first few elements of a Series. It lets you retrieve the top n values, with the default being the first 10 elements. Whether the Series contains positive, negative, or zero values, the method consistently returns the first n elements in the order they appear in the Series.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars Series tail() Usage & Examples
- Polars Series min() – Explained by Examples
- Polars Series max() Function
- Convert Polars Series to List
- Polars Series tail() Usage & Examples
- Polars Series filter() Method with Examples
- Polars Series list.join() Function with Examples
- Polars Series explode() – Explained by Examples
- How to Replace Certain Values in a Polars Series?
- How to Convert Polars Series to Pandas Series in Python?
<a id="h-reference"></a>
