0

A follow up from an already answered question, is it possible to Search and replace column names in a LazyFrame? I am doing this as a workaround (based on the linked answer by ritchie46, and thanks for that!):

df = df.lazy().collect()
df.columns = list(map(lambda x: x.replace("Total", ""), df.columns)) 
DBOak
  • 43
  • 7

1 Answers1

0
(pl.DataFrame({
    "Total_foo": [1],
    "bar_Total": [2],
    "other": [3],
}).lazy()
  .select(
     pl.all().map_alias(lambda name: name.replace("Total", ""))
 )).collect()

shape: (1, 3)
┌──────┬──────┬───────┐
│ _foo ┆ bar_ ┆ other │
│ ---  ┆ ---  ┆ ---   │
│ i64  ┆ i64  ┆ i64   │
╞══════╪══════╪═══════╡
│ 1    ┆ 2    ┆ 3     │
└──────┴──────┴───────┘
ritchie46
  • 10,405
  • 1
  • 24
  • 43