How to convert polars.DataFrame to list?
- User
How to convert polars.DataFrame to list?
Assistant
Okay, you want to convert a
polars.DataFrameinto a standard Pythonlist. There are a few ways to do this, depending on how you want the data structured in the list.Here are the most common methods:
- List of Tuples: Each item in the list is a tuple representing a row. This is often the most direct conversion of rows.
- List of Lists: Each item in the list is a list representing a row. Similar to list of tuples, but using lists. This is often achieved by converting to NumPy first.
- List of Dictionaries: Each item in the list is a dictionary where keys are column names and values are row values for that column. This preserves column name information.
Let's look at how to do each:
First, create a sample Polars DataFrame:
import polars as pl # Create a sample DataFrame df = pl.DataFrame({ "col1": [1, 2, 3], "col2": ["A", "B", "C"], "col3": [True, False, True], }) print("Original DataFrame:") print(df)Original DataFrame: shape: (3, 3) ┌──────┬──────┬──────┐ │ col1 ┆ col2 ┆ col3 │ │ --- ┆ --- ┆ --- │ │ i64 ┆ str ┆ bool │ ╞══════╪══════╪══════╡ │ 1 ┆ A ┆ true │ │ 2 ┆ B ┆ false│ │ 3 ┆ C ┆ true │ └──────┴──────┴──────┘Now, let's convert it using the different methods:
Method 1: List of Tuples (Using
.rows())The
.rows()method returns an iterator of tuples, where each tuple represents a row. You can convert this iterator directly to a list.list_of_tuples = list(df.rows()) print("\nMethod 1: List of Tuples") print(list_of_tuples)Method 1: List of Tuples [(1, 'A', True), (2, 'B', False), (3, 'C', True)]Method 2: List of Lists (Using
.to_numpy().tolist())Polars doesn't have a direct
.to_list_of_lists()method for the whole DataFrame, but you can first convert it to a NumPy array and then use NumPy's.tolist()method.# Ensure you have numpy installed (pip install numpy) import numpy as np list_of_lists = df.to_numpy().tolist() print("\nMethod 2: List of Lists (via NumPy)") print(list_of_lists)Method 2: List of Lists (via NumPy) [[1, 'A', True], [2, 'B', False], [3, 'C', True]]Method 3: List of Dictionaries (Using
.to_dict('row'))The
.to_dict()method can structure the output in different ways. Using'row'as the argument gives you a list where each element is a dictionary representing a row, with column names as keys.list_of_dicts = df.to_dict('row') print("\nMethod 3: List of Dictionaries") print(list_of_dicts)Method 3: List of Dictionaries [{'col1': 1, 'col2': 'A', 'col3': True}, {'col1': 2, 'col2': 'B', 'col3': False}, {'col1': 3, 'col2': 'C', 'col3': True}]Choosing the Right Method:
- List of Tuples (
list(df.rows())): This is generally the most memory-efficient and often the fastest way to get row data as Python sequences without column names. It's good for simple iteration over rows. - List of Lists (
df.to_numpy().tolist()): Useful if you're already familiar with NumPy and prefer lists over tuples for rows. Requires NumPy. - List of Dictionaries (
df.to_dict('row')): This is the best option if you need to keep track of which value corresponds to which column name for each row. It's less memory-efficient than tuples/lists as it duplicates the column names for every row.
Remember that converting a large Polars DataFrame entirely into a standard Python list structure can be memory-intensive and might lose some of the performance benefits of using Polars. If possible, try to perform operations within Polars itself.
- User
I have a single row DataFrame, how can I convert it to a list?
Assistant
Okay, if you have a
polars.DataFramethat you know contains only a single row, you want to extract the values from that row and put them into a single Python list.The most direct way to do this is using the
.row()method and then converting the resulting tuple to a list.Here's how:
- Use
df.row(0): This method specifically returns the data of the row at the given index as a Python tuple. Since your DataFrame has only one row, its index is0. - Convert the tuple to a list: Use the standard Python
list()constructor on the tuple returned by.row(0).
Let's see an example:
import polars as pl # Create a sample DataFrame with a single row df_single_row = pl.DataFrame({ "id": [101], "name": ["Alice"], "is_active": [True], "value": [42.5] }) print("Original Single Row DataFrame:") print(df_single_row) # Convert the single row to a list # df.row(0) returns a tuple: (101, 'Alice', True, 42.5) # list(...) converts that tuple to a list: [101, 'Alice', True, 42.5] row_as_list = list(df_single_row.row(0)) print("\nConverted List:") print(row_as_list) print(type(row_as_list)) print(type(row_as_list[0])) # Check type of elementsOutput:
Original Single Row DataFrame: shape: (1, 4) ┌─────┬───────┬─────────┬───────┐ │ id ┆ name ┆ is_active ┆ value │ │ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ str ┆ bool ┆ f64 │ ╞═════╪═══════╪─────────╪───────╡ │ 101 ┆ Alice ┆ true ┆ 42.5 │ └─────┴───────┴─────────┴───────┘ Converted List: [101, 'Alice', True, 42.5] <class 'list'> <class 'int'> # Or str, bool, float depending on element indexThis is the most direct and recommended way when you specifically need to convert one known row into a flat Python list.
Alternative (using NumPy):
You could also go via NumPy, similar to the multi-row case, and then extract the first element:
# Ensure numpy is installed (pip install numpy) import numpy as np # This returns a list of lists: [[101, 'Alice', True, 42.5]] list_of_lists = df_single_row.to_numpy().tolist() # Get the first (and only) list from the list of lists row_as_list_np = list_of_lists[0] print("\nConverted List (via NumPy):") print(row_as_list_np)This also works, but
list(df.row(0))is generally more idiomatic and doesn't require the NumPy dependency just for this simple conversion.- Use
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.