Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rename all columns in dataframe to lowercase

Given a polars dataframe I want to rename all columns to their lowercase version. As per polars.Expr.name.to_lowercase we can do

import polars as pl
pl.DataFrame([{'CCY': 'EUR', 'Qty': 123},
              {'CCY': 'USD', 'Qty': 456}]).with_columns(pl.all().name.to_lowercase())

but this duplicates the data (as it keeps the original column names).

Conceptually, I am looking for something like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pl.DataFrame(...).rename({c: c.name.to_lowercase() for c in pl.all()})

But this doesn’t work since pl.all() is not iterable.

>Solution :

select rather than with_columns:

df.select(pl.all().name.to_lowercase())

Output:

┌─────┬─────┐
│ ccy ┆ qty │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════╪═════╡
│ EUR ┆ 123 │
│ USD ┆ 456 │
└─────┴─────┘

Note that you could also use your envisioned approach with cs.expand_selector:

import polars.selectors as cs

df.rename({c: c.lower() for c in cs.expand_selector(df, cs.all())})

although in the case of all columns could be replaced by a simple:

df.rename(str.lower)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading