In Polars (a high-performance DataFrame library similar to pandas), accessing a specific row using df.row(index) returns a tuple containing the values from that row.
However, sometimes it’s more useful to have that row in dictionary form, where you can see the column names as keys and the row values as values. This is especially handy for readability or when converting data to JSON or other formats. In this article, I will demonstrate how to convert a Polars row to a dictionary format.
Key Points–
- Use
row(index)to retrieve a specific row as a tuple in Polars. - You can loop through all rows using
iter_rows()to convert each row to a dictionary. - To convert the first, last, or middle row, use appropriate indexing with
row(). - Polars does not have a dedicated
Rowobject like pandas; instead,row(index)returns a tuple of values from that row. - You can convert a Polars row to a dictionary by zipping the column names with the values from row(index).
- The
df.columnsattribute provides the list of column names needed for the zip operation. - Use
dict(zip(df.columns, df.row(i)))to convert a single row at indexito a dictionary. - This method is useful for exporting single rows in a format compatible with standard Python operations.
<a id="h-usage-of-polars-row-object-to-dictionary"></a>
Usage of Polars Row Object to Dictionary
To convert a row in a Polars DataFrame to a dictionary, use the row(index) method to get the row as a tuple and combine it with the column names using zip() and dict().
To run some examples of how to convert polars row objects to dictionaries, let’s create a Polars DataFrame.
import polars as pl
technologies= {
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fees' :[22000,25000,23000,24000,26000],
'Duration':['30days','50days','35days', '40days','55days'],
'Discount':[1000,2300,1000,1200,2500]
}
df = pl.DataFrame(technologies)
print("Original DataFrame:\n", df)
Yields below output.
You can convert the first row of a Polars DataFrame to a dictionary using the row(0) method, followed by zipping with the column names.
# Convert first row to dictionary
df2 = dict(zip(df.columns, df.row(0)))
print("First row as dictionary:\n", df2)
Yields below output.
<a id="h-convert-last-row-to-dictionary"></a>
Convert Last Row to Dictionary
To convert the last row of the Polars DataFrame to a dictionary, you can do something similar by using the index -1 with row().
# Convert last row to dictionary
df2 = dict(zip(df.columns, df.row(-1)))
print("Last row as dictionary:\n", df2)
# Output:
# Last row as dictionary:
# {'Courses': 'Pandas', 'Fees': 26000, 'Duration': '55days', 'Discount': 2500}
<a id="h-loop-through-all-rows-and-convert-to-dictionary"></a>
Loop Through all Rows and Convert to Dictionary
You can loop through all rows in the Polars DataFrame and convert each one to a dictionary.
# Loop through all rows and convert each to a dictionary
for i in range(df.height):
row_dict = dict(zip(df.columns, df.row(i)))
print(row_dict)
# Using DataFrame.iter_rows()
for row in df.iter_rows(named=True):
print(row)
# Output:
# {'Courses': 'Spark', 'Fees': 22000, 'Duration': '30days', 'Discount': 1000}
# {'Courses': 'PySpark', 'Fees': 25000, 'Duration': '50days', 'Discount': 2300}
# {'Courses': 'Hadoop', 'Fees': 23000, 'Duration': '35days', 'Discount': 1000}
# {'Courses': 'Python', 'Fees': 24000, 'Duration': '40days', 'Discount': 1200}
# {'Courses': 'Pandas', 'Fees': 26000, 'Duration': '55days', 'Discount': 2500}
<a id="h-convert-a-row-by-condition-e-g-course-python"></a>
Convert a Row by Condition (e.g. Course == "Python")
To convert a row based on a condition (like Course == "Python") to a dictionary in Polars, you can filter the DataFrame first and then use row(0) to extract it.
# Filter the row where Course == "Python"
filtered_df = df.filter(pl.col("Courses") == "Python")
# Convert the filtered row to a dictionary (assuming there's only one match)
if filtered_df.height > 0:
row_dict = dict(zip(filtered_df.columns, filtered_df.row(0)))
print("Row where Courses == 'Python' as dictionary:\n", row_dict)
else:
print("No matching row found.")
# Output:
# Row where Courses == 'Python' as dictionary:
# {'Courses': 'Python', 'Fees': 24000, 'Duration': '40days', 'Discount': 1200}
<a id="h-create-list-of-dictionaries-from-all-rows"></a>
Create List of Dictionaries from All Rows
To convert the middle row of a Polars DataFrame to a dictionary, you can calculate the middle index using integer division and use row().
# Get the middle row index
middle_index = df.height // 2
# Convert the middle row to a dictionary
df2 = dict(zip(df.columns, df.row(middle_index)))
print("Middle row as dictionary:\n", df2)
# Output:
# Middle row as dictionary:
# {'Courses': 'Hadoop', 'Fees': 23000, 'Duration': '35days', 'Discount': 1000}
<a id="h-convert-a-row-to-dictionary-and-access-a-field"></a>
Convert a Row to Dictionary and Access a Field
You can convert a specific row to a dictionary and then access a particular field from it just like you would with any Python dictionary.
# Filter the row where Courses == "Python"
filtered_df = df.filter(pl.col("Courses") == "Python")
# Convert to dictionary and access a field
if filtered_df.height > 0:
row_dict = dict(zip(filtered_df.columns, filtered_df.row(0)))
fees_value = row_dict["Fees"]
print("Row dictionary:", row_dict)
print("Accessed 'Fees' field:", fees_value)
else:
print("No matching row found.")
# Output:
# Row dictionary: {'Courses': 'Python', 'Fees': 24000, 'Duration': '40days', 'Discount': 1200}
# Accessed 'Fees' field: 24000
<a id="h-conclusion"></a>
Conclusion
In conclusion, converting rows from a Polars DataFrame to dictionaries is a straightforward yet effective approach to handling structured data in native Python, ideal for API usage, JSON serialization, or whenever a more flexible format is needed.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Make a Constant Column in Polars
- Extract Value of Polars Literal
- Polars Filter by Column Value
- Convert Polars String to Integer
- Polars Rename Columns to Lowercase
- How to Select Last Column of Polars DataFrame
- How to Change Position of a Column in Polars
- Reorder Columns in a Specific Order Using Polars
- How to Convert a Polars DataFrame to Python List?
- Append or Concatenate Two DataFrames in Polars
- How to Add a Column with Numerical Value in Polars
- Get First N Characters from a String Column in Polars
<a id="h-references"></a>
