1

I have the following polars dataframe and I wanted to select the last column dynamically.

>>> import polars as pl
>>> 
>>> df = pl.DataFrame({
...     "col1": [1, 2],
...     "col2": ["2", "3"],
...     "col3": [3, 4]
... })
>>> 
>>> df
shape: (2, 3)
┌──────┬──────┬──────┐
│ col1 ┆ col2 ┆ col3 │
│ ---  ┆ ---  ┆ ---  │
│ i64  ┆ str  ┆ i64  │
╞══════╪══════╪══════╡
│ 1    ┆ 2    ┆ 3    │
│ 2    ┆ 3    ┆ 4    │
└──────┴──────┴──────┘
>>> # How to select col3? which is the last column in df

How can i do this in polars?. I can do df.iloc[:,-1:] to select the last column if it's a pandas dataframe.

Additional info:

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=11, micro=0, releaselevel='final', serial=0)
>>> import polars
>>> polars.__version__
'0.18.3'
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46

4 Answers4

3

To aid in operatons like these, a polars.selectors module was introduced recently. You can simply use last from this module:

df.select(cs.last())
shape: (2, 1)
┌──────┐
│ col3 │
│ ---  │
│ i64  │
╞══════╡
│ 3    │
│ 4    │
└──────┘
Wayoshi
  • 1,688
  • 1
  • 7
1

df.columns is the list of column names, you can index this to get the last column name.

df.get_column(df.columns[-1])
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

get_columns() is useful here.

import polars as pl
df = pl.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]})
df.get_columns()[-1]

shape: (3,)
Series: 'bar' [i64]
[
    4
    5
    6
]
yk1031
  • 11
  • 1
0

There is also pl.first() / pl.last() e.g.

df.select(pl.last())
shape: (2, 1)
┌──────┐
│ col3 │
│ ---  │
│ i64  │
╞══════╡
│ 3    │
│ 4    │
└──────┘
jqurious
  • 9,953
  • 1
  • 4
  • 14