5

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.

1
  • It would be better if you can also share the error. Commented Jan 8, 2023 at 16:13

3 Answers 3

5

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")
Sign up to request clarification or add additional context in comments.

Comments

3

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 │
└─────┴──────┘

Comments

0

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 │
└─────┴──────┘

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.