In Polars, the DataFrame.width
property returns the total number of columns in the DataFrame. It provides a quick and efficient way to examine the structure of your dataset without needing to load or view the full DataFrame.
In this article, I will explain the Polars DataFrame width
Property by using syntax. Through clear examples, you’ll learn how it returns an integer representing the number of columns in a Polars DataFrame.
Key Points –
width
is a property (not a method) of Polars DataFrame.- It returns an integer representing the column count.
- It works on both non-empty and empty DataFrames.
- When a DataFrame has no columns,
width
returns0
. - It does not include the number of rows, only columns.
width
can be used in loops or scripts to check for column consistency across multiple DataFrames.- Adding or dropping columns will change the
width
value dynamically. - If a DataFrame has columns but no rows, width still reflects the column count.
Syntax of Polars DataFrame width Property
Let’s know the syntax of the width property.
# Syntax of width
property DataFrame.width: int
Return Value
This function returns an integer (int) representing the number of columns in the Polars DataFrame.
Usage of Polars DataFrame width Property
The width
property in Polars is a simple and efficient way to get the number of columns in a DataFrame. It is particularly useful for checking the structure of your data quickly.
To run some examples of the Polars DataFrame width Property, let’s create a Polars DataFrame.
import polars as pl
technologies= {
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fee' :[22000, 25000, 23000, 24000, 35000],
'Discount':[1000, 2300, 1000, 1200, 2200],
'Duration':['35days','40days','65days','50days','60days']
}
df = pl.DataFrame(technologies)
print("Original DataFrame:\n", df)
Yields below output.
To get the number of columns in a Polars DataFrame, you can use the width
property.
# Get number of columns
print("Number of columns (width):", df.width)
Here,
df.width
returns an int representing the number of columns in the DataFrame.- In this case, the DataFrame includes the columns:
'Courses'
,'Fee'
,'Discount'
, and'Duration'
.
Using df.width After Dropping a Column
When you drop a column from a Polars DataFrame, the width
property immediately reflects the reduced column count. This is consistent whether the DataFrame contains data or is empty.
# Drop the 'Discount' column
df_dropped = df.drop("Discount")
print("After dropping 'Discount', number of columns:", df_dropped.width)
# Output:
# After dropping 'Discount', number of columns: 3
Here,
- Initially,
df
has 4 columns:'Courses'
,'Fee'
,'Discount'
, and'Duration'
. - After
df.drop("Discount")
, the new DataFramedf_dropped
has only 3 columns. - Using
width
confirms the column count before and after the drop.
Conditional Logic Based on Width Property
Applying conditional logic based on the width (i.e., number of columns) of a Polars DataFrame can be useful when handling dynamic data, allowing you to perform different actions depending on how many columns are present.
# Check number of columns and act accordingly
if df.width > 2:
print("This DataFrame has more than 2 columns.")
elif df.width == 2:
print("This DataFrame has exactly 2 columns.")
else:
print("This DataFrame has fewer than 2 columns.")
# Output:
# This DataFrame has more than 2 columns.
Here,
df.width
gives the number of columns.- You can use standard Python
if
,elif
, andelse
to apply conditional logic. - This helps in situations where you want to: Drop columns if too many exist. Combine columns conditionally. Trigger different processing logic.
Using width on an Empty DataFrame
Using the width
property on an empty Polars DataFrame (with 0 rows) still returns the correct number of defined columns. This makes it helpful for validating the structure of a DataFrame, even when it contains no data.
import polars as pl
# Create an empty DataFrame
empty_df = pl.DataFrame()
# Get the number of columns
print("Number of columns in empty DataFrame:", empty_df.width)
# Output:
# Number of columns in empty DataFrame: 0
Here,
pl.DataFrame()
creates an empty DataFrame with no columns and no rows.width
returns the number of columns, so for an empty DataFrame, this will be0
.
Adding a new column Using width
When you add a new column to a Polars DataFrame, the width property automatically updates to reflect the increased column count.
import polars as pl
df = pl.DataFrame({
'Courses': ["Spark", "PySpark", "Hadoop"],
'Fee': [22000, 25000, 23000]
})
# Add a new column
df = df.with_columns(
pl.Series('Discount',[1000, 2300, 1000]) )
print("New width:", df.width)
# Output:
# New width: 3
Here,
with_columns()
returns a new DataFrame (Polars is immutable by default).- The original
df
remains unchanged unless reassigned.
Conclusion
In conclusion, the DataFrame.width
property in Polars is a simple yet powerful tool to quickly determine the number of columns in your dataset. Whether you’re validating schema, building dynamic data pipelines, or applying conditional logic, width
offers a lightweight and efficient way to understand and work with the structure of your DataFrame.
Happy Learning!!
Related Articles
- 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 shape – Explained by Examples
- Polars DataFrame replace_column() – by Examples