I'm looking for the recommended way to select an individual row of a polars.DataFrame
by row number: something largely equivalent to pandas.DataFrame
's .iloc[[n]]
method for a given integer n
.
For polars imported as pl
and a polars DataFrame df
, my current approach would be:
# for example
n = 3
# create row count, filter for individual row, drop the row count.
new_df = (
df.with_row_count()
.filter(pl.col('row_nr') == n)
.select(pl.exclude('row_nr'))
)
I'm migrating from Pandas, and I've read the Pandas-to-Polars migration guide, but a slick solution to this specific case wasn't addressed there. Edit: to clarify, I am looking for an approach that returns a polars.DataFrame
object for the chosen row.
Does anyone have something slicker?
df[3]
works - but wouldn't be "recommended" if you were composing larger expressions.polars.DataFrame.with_row_index