Index Slicing
Unlike tools like pandas which has indexing, Polars doesn't use indexing however, we can use indeces to fetch data and value. Let's demonstrate how this can be achieved.
import polars as pl
data = pl.read_csv("data/employees.csv")
print(data.head())
data.shape
row()
We can use the
data.row(0)
We notice that we now have the first row returned as a tuple. We can then fetch the specific item.
data.row(0)[5]
rows as dictionary
We can also return each row of data as a dictionary where the column names represent the key for the dictionary.
# Get a single row as a dictionary
row_dict = data.row(0, named=True)
print(row_dict)
item()
The item() object is the method used to extract values directly from the dataframe.
value = data.filter(pl.col("id") == 3).select("age").item()
value