DEV Community

Cover image for View and Inspect Polars DataFrames
Muhammad Adil
Muhammad Adil

Posted on Originally published at adilaidev.com

View and Inspect Polars DataFrames

Polars truncates output by default so a print does not flood your terminal. That is great until you actually want to see everything. Here is how to inspect a frame, print all rows, add row numbers, and pull data out.

Print all rows and columns

Raise the display limits with pl.Config. Pass -1 to show every row. Wrap it in a context manager if you only want it for one print.

  • pl.Config.set_tbl_rows(-1) # show all rows
  • pl.Config.set_tbl_cols(-1) # show all columns
  • print(df)
  • # temporary, just for this block:
  • with pl.Config(tbl_rows=-1):
  • print(df)

Add row numbers

with_row_index adds a 0-based index column, which is the Polars way to get line numbers for a DataFrame. (In older versions this was with_row_count.)

  • df = df.with_row_index("row")
  • # start at 1:
  • df = df.with_row_index("row", offset=1)

Convert a column to a list

Pull a column out as a plain Python list with to_list. This answers using to_list() in Polars.

  • values = df["a"].to_list()
  • # or explicitly:
  • values = df.get_column("a").to_list()

Quick looks

A few one-liners for a fast read on any frame.

  • df.head(5)
  • df.tail(5)
  • df.sample(5)
  • df.describe()
  • df.schema # column names and types
  • df.shape # (rows, columns)

Stack frames: hstack and vstack

hstack adds columns side by side, vstack stacks rows. For many frames at once, pl.concat is cleaner. This is what hstack does in Polars.

  • df.hstack([other]) # add columns (same number of rows)
  • df.vstack(other) # add rows (same columns)
  • pl.concat([df1, df2]) # stack many frames by rows
  • pl.concat([df1, df2], how="horizontal") # side by side

To build the frames you are inspecting, see creating DataFrames; to narrow them down, see filtering rows. Everything else is in the Python Polars cheat sheet.


This post was originally published on my site. Read the full article and more →

Top comments (0)