Reputation: 160
I have a polars dataframe of species, 89 date columns and 23 unique species. The goal is aggregation by a groupby as well as a range of columns. iloc would be the way to do this in pandas, but the select option doesn't seem to work the way I want it to.
In pandas:
gb = df.groupby(["Common_name"]).agg(dict(zip(df.iloc[:, 32:103].columns, ["mean"] * len(df.iloc[:, 32:103]))))
is there a way to select column indices in polars?
Upvotes: 7
Views: 5873
Reputation: 10464
Example data.
import polars as pl
df = pl.DataFrame({
"a": [1, 2, 3],
"b": [True, False, False],
"c": ["a", "b", "c"],
})
Since polars 0.20.26 there exists pl.nth
as idiomatic way to select columns by index.
Using a single index.
df.select(pl.nth(0))
shape: (3, 1)
┌─────┐
│ a │
│ --- │
│ i64 │
╞═════╡
│ 1 │
│ 2 │
│ 3 │
└─────┘
Using a list of indices (introduced in version 0.20.27).
df.select(pl.nth([0, 2]))
shape: (3, 2)
┌─────┬─────┐
│ a ┆ c │
│ --- ┆ --- │
│ i64 ┆ str │
╞═════╪═════╡
│ 1 ┆ a │
│ 2 ┆ b │
│ 3 ┆ c │
└─────┴─────┘
Using a range of indices. (introduced in version 0.20.27).
df.select(pl.nth(range(0, 2)))
shape: (3, 2)
┌─────┬───────┐
│ a ┆ b │
│ --- ┆ --- │
│ i64 ┆ bool │
╞═════╪═══════╡
│ 1 ┆ true │
│ 2 ┆ false │
│ 3 ┆ false │
└─────┴───────┘
Upvotes: 6