In pandas we can select columns from dataframe using df_new = df[["A","B"]]. How are columns selected in polars?
I tried usng df_new = df.get_columns(["A","B"]), but an error is raised.
In pandas we can select columns from dataframe using df_new = df[["A","B"]]. How are columns selected in polars?
I tried usng df_new = df.get_columns(["A","B"]), but an error is raised.
I suggest you to read or skim through Polars introduction in the User guide
It is nicely written with some good examples.
You can choose columns by using Polars select statement
df_new = df.select("A", "B")
In polars, you would use pl.DataFrame.select and pass the column names of interest directly as parameters.
import polars as pl
df = pl.DataFrame({
"id": [1, 2, 3],
"name": ["John", "Jane", "Jake"],
"age": [16, 20, 25],
})
df.select("id", "name")
shape: (3, 2)
┌─────┬──────┐
│ id ┆ name │
│ --- ┆ --- │
│ i64 ┆ str │
╞═════╪══════╡
│ 1 ┆ John │
│ 2 ┆ Jane │
│ 3 ┆ Jake │
└─────┴──────┘
Starting from Polars 0.18.1 You can use polars.selectors.by_name to select all columns matching the given names.
>>> import polars as pl
>>> import polars.selectors as cs
>>>
>>> df = pl.DataFrame(
... {
... "id": [1, 2, 3],
... "name": ["John", "Jane", "Jake"],
... "else": [10.0, 20.0, 30.0],
... }
... )
>>>
>>> print(df.select(cs.by_name('id', 'name')))
shape: (3, 2)
┌─────┬──────┐
│ id ┆ name │
│ --- ┆ --- │
│ i64 ┆ str │
╞═════╪══════╡
│ 1 ┆ John │
│ 2 ┆ Jane │
│ 3 ┆ Jake │
└─────┴──────┘