Question Details

No question body available.

Tags

python dataframe python-polars

Answers (4)

Accepted Answer Available
Accepted Answer
October 29, 2025 Score: 5 Rep: 359 Quality: High Completeness: 50%

I used a regular expression to solve this issue. In Polars you signal regular expressions with the start and end symbols ^ and $ and the needs to be escaped so the full solution looks like

python import polars as pl df = pl.DataFrame({"A": [1, 2], "B": [3, None], "
": [4, 5]}) print(df.select(pl.col(r"^\$"))) # --> prints only the "" column
October 30, 2025 Score: 3 Rep: 15,304 Quality: Low Completeness: 60%

You could use pl.DataFrame.getitem as well:

df[[""]]

Output:

shape: (2, 1) ┌─────┐ │
│ │ --- │ │ i64 │ ╞═════╡ │ 4 │ │ 5 │ └─────┘
October 30, 2025 Score: 0 Rep: 24,776 Quality: Medium Completeness: 80%

Update: Polars 1.38.1

cs.byname treating * as a wildcard was considered a bug and fixed by pull/26437

df.drop(cs.byname(""))

shape: (2, 2)

┌─────┬──────┐

│ A ┆ B │

│ --- ┆ --- │

│ i64 ┆ i64 │

╞═════╪══════╡

│ 1 ┆ 3 │

│ 2 ┆ null │

└─────┴──────┘


Original answer

It seems only the col() docs mention
being a wildcard, i.e. pl.col("") is the same as pl.all()

I believe df.drop("
") ends up as df.drop(pl.col("")) so bare strings go through the wildcard / regex parsing.

df.drop(r"^A.$")

shape: (2, 2)

┌──────┬─────┐

│ B ┆

│ --- ┆ --- │

│ i64 ┆ i64 │

╞══════╪═════╡

│ 3 ┆ 4 │

│ null ┆ 5 │

└──────┴─────┘

There is a growing Selectors API, but it doesn't seem to contain a direct approach either.

I thought cs.by_name() would only take "literal names" but it seems to be parsed the same as col():

import polars.selectors as cs

df.select(cs.by_name("
"))

shape: (2, 3)

┌─────┬──────┬─────┐

│ A ┆ B ┆

│ --- ┆ --- ┆ --- │

│ i64 ┆ i64 ┆ i64 │

╞═════╪══════╪═════╡

│ 1 ┆ 3 ┆ 4 │

│ 2 ┆ null ┆ 5 │

└─────┴──────┴─────┘

>>> print(cs.by_name(""))
cs.all()

contains / startswith / endswith regex escape their inputs.

>>> print(cs.starts_with(""))
cs.matches("^(\).$")
>>> print(cs.contains(""))
cs.matches("^.(\).$")
df.drop(cs.contains(""))

shape: (2, 2)

┌─────┬──────┐

│ A ┆ B │

│ --- ┆ --- │

│ i64 ┆ i64 │

╞═════╪══════╡

│ 1 ┆ 3 │

│ 2 ┆ null │

└─────┴──────┘

I would have expected matches to produce an invalid pattern error or such:

df.drop(cs.matches(""))

shape: (0, 0)

┌┐

╞╡

└┘

There are also some general selectors which could exclude
(and others):

df.select(cs.alphanumeric())

shape: (2, 2)

┌─────┬──────┐

│ A ┆ B │

│ --- ┆ --- │

│ i64 ┆ i64 │

╞═════╪══════╡

│ 1 ┆ 3 │

│ 2 ┆ null │

└─────┴──────┘

October 30, 2025 Score: 0 Rep: 99 Quality: Low Completeness: 30%

You could use

df[:, [index]]

if you know the column position.