Preview data
Quickly inspect the beginning of a newly loaded or transformed table.
Polars · Module
Returns the first n rows of a Polars DataFrame. By default, it returns five rows; a negative n omits that many rows from the end.
Call head on a DataFrame to obtain a new DataFrame containing the first requested rows. The default is five rows; passing a negative number instead excludes that many rows from the end, which is useful when inspecting nearly all data while omitting a tail.
DataFrame.head(*n: int = 5*) → DataFrame| Name | Type | Description |
|---|---|---|
n | int = 5 | Number of rows to return. A negative value returns all rows except the last abs(n). |
A DataFrame containing the selected leading rows.
import polars as pl
df = pl.DataFrame({"id": [1, 2, 3], "name": ["Ada", "Linus", "Grace"]})
print(df.head(2))Creates a DataFrame and returns its first two rows.
Quickly inspect the beginning of a newly loaded or transformed table.
Check whether an operation produced the expected leading records.
Display nearly all rows while excluding a known trailing portion.
DataFrame.head return, and what is its default?DataFrame.head returns the first n rows as a DataFrame, with n defaulting to 5. It is useful for quickly inspecting the beginning of a table.
Follow-up: What does head(-3) do? It returns all rows except the final three.
n parameter?The method accepts one parameter, n, which specifies the number of rows to return. The result preserves the DataFrame form rather than returning a Series or scalar.
Follow-up: How would you inspect only one column? Select that column first, then call head on the resulting DataFrame.
n change the behavior?A negative n is supported and changes the operation to exclude rows from the end. For example, head(-3) returns all rows other than the last three.
Follow-up: What happens if the exclusion count is larger than the DataFrame? Verify the behavior with a small test DataFrame rather than assuming a fixed output shape.
polars.DataFrame.limitpolars.headpolars.Expr.headpolars.dataframe.group_by.GroupBy.head