To convert a Python list into a Polars DataFrame, you can use the pl.DataFrame() constructor. This conversion transforms a native Python data structure, whether it’s a simple list, a list of lists, tuples, or dictionaries, into a Polars DataFrame. Polars DataFrames provide a high-performance, efficient, and well-structured format tailored for managing tabular data, enabling you to turn basic Python lists into fast and expressive table-like structures ideal for data analysis and manipulation. In this article, I will explain convert a Python list to polars DataFrame.
Key Points –
- Polars DataFrame can be created directly from various Python list structures (list of dicts, list of lists, list of tuples).
- Use
pl.DataFrame()constructor to convert Python lists into Polars DataFrames. - List of lists or tuples can be converted into a DataFrame by specifying column names via the
schemaparameter. - A list of lists can be converted by specifying a schema to assign column names.
- A list of dictionaries can be passed directly to
pl.DataFrame()to infer both structure and column names. - Multiple lists (each representing a column) can be passed as a dictionary with keys as column names.
<a id="h-list-of-dictionaries"></a>
List of Dictionaries
To convert a basic Python list into a Polars DataFrame, place the list in a dictionary where the keys represent the column name, and pass it to the pl.DataFrame() constructor.
import polars as pl
data = [{"Courses": "Spark", "Fees": 30000}, {"Courses": "Pandas", "Fees": 25000}]
df = pl.DataFrame(data)
print(df)
Yields below output.
<a id="h-list-of-lists-with-column-names"></a>
List of Lists with Column Names
To create a Polars DataFrame from a list of lists with specified column names, pass the data together with a schema argument (a list of column names) to the pl.DataFrame() constructor.
import polars as pl
data = [
["Spark", 30000],
["Pandas", 25000],
["Hyperion", 40000]
]
# Assign column names using schema
df = pl.DataFrame(data, schema=["Courses", "Fees"])
print(df)
<a id="h-list-of-tuples-to-polars-dataframe"></a>
List of Tuples to Polars DataFrame
You can convert a list of tuples into a Polars DataFrame by specifying the column names. A list of tuples is similar to a list of lists, except each row is a tuple instead of a list. When creating the Polars DataFrame, you provide the column names using the schema parameter.
import polars as pl
data = [
("Spark", 30000),
("Pandas", 25000),
("Hyperion", 40000)
]
# Assign column names using schema
df = pl.DataFrame(data, schema=["Courses", "Fees"])
print(df)
<a id="h-single-list-1d-list-as-one-column"></a>
Single List (1D list) as One Column
If you have a simple one-dimensional list and want to convert it into a Polars DataFrame with a single column, wrap the list inside a dictionary where the key is the desired column name. This approach transforms the basic list into a DataFrame containing one column.
import polars as pl
# Single list (1D)
data = ["Spark", "Pandas", "Polars"]
# Create DataFrame with one column named "Courses"
df = pl.DataFrame({"Courses": data})
print(df)
# Output:
# shape: (3, 1)
┌─────────┐
│ Courses │
│ --- │
│ str │
╞═════════╡
│ Spark │
│ Pandas │
│ Polars │
└─────────┘
<a id="h-multiple-lists-as-columns"></a>
Multiple Lists as Columns
When you have multiple Python lists, each corresponding to a column, you can create a Polars DataFrame by passing a dictionary where the keys are the column names and the values are the respective lists. This allows you to combine separate lists into one structured DataFrame.
import polars as pl
# Multiple lists — each list is a column
courses = ["Spark", "Pandas", "Polars"]
fees = [30000, 25000, 35000]
# Create DataFrame by passing a dictionary
df = pl.DataFrame({
"Courses": courses,
"Fees": fees
})
print(df)
# Output:
# shape: (3, 2)
┌─────────┬───────┐
│ Courses ┆ Fees │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════════╪═══════╡
│ Spark ┆ 30000 │
│ Pandas ┆ 25000 │
│ Polars ┆ 35000 │
└─────────┴───────┘
<a id="h-nested-lists-as-a-single-column"></a>
Nested Lists as a Single Column
If you have nested lists (a list of lists) and want each inner list to be stored as a single cell within one column of a Polars DataFrame, you can take advantage of Polars' support for list-type columns. This allows each inner list to remain intact as a single element in the DataFrame.
import polars as pl
# Nested list (list of lists)
data = [[1, 2, 3], [4, 5], [6]]
# Create DataFrame with one column named "Numbers"
df = pl.DataFrame({
"Numbers": data
})
print(df)
# Output:
# shape: (3, 1)
┌───────────┐
│ Numbers │
│ --- │
│ list[i64] │
╞═══════════╡
│ [1, 2, 3] │
│ [4, 5] │
│ [6] │
└───────────┘
<a id="h-conclusion"></a>
Conclusion
In conclusion, converting Python lists to Polars DataFrames is a simple yet powerful step toward efficient data handling. With just a few lines of code, you can turn various list formats, whether 1D, nested, or structured, into a fast, flexible, and expressive DataFrame. Polars makes it easy to go from raw data to structured analysis, enabling better performance and cleaner workflows.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars DataFrame Drop Nans
- Replace Value by Null in Polars
- Add Row of Column Totals in Polars
- Polars DataFrame Columns Selection
- How to use isin in Polars DataFrame?
- Polars Delete a Column from DataFrame
- Polars String Manipulation of Cell Contents
- Polars DataFrame rows() Method with Examples
- Polars String Concatenation of Two Existing Columns
- How to Get the Name of an Aliased Expression in Polars?
- Polars DataFrame with_columns() Function with Examples
- How to Change the First Letter of the Column Name to Uppercase in Polars?
<a id="h-reference"></a>
