Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove all whitespaces from the headers of a polars dataframe

I'm reading some csv files where the column headers are pretty annoying: they contain whitespaces, tabs, etc.

A      B    C      D    E
CD  E   300 0   0   0
CD  E   1071    0   0   0
K   E   390 0   0   0

I want to read the file, then remove all whitespaces and/or tabs from the column names. Currently I do

import polars as pl
file_df = pl.read_csv(csv_file,
                      comment_prefix='#',
                      separator='\t')
file_df = file_df.rename(lambda column_name: column_name.strip())

Is this the "polaric" way to do it? I'm not a big fan of lambdas, but if the only other solution is to write a function just for this, I guess I'll stick to lambdas.

like image 456
DeltaIV Avatar asked Dec 22 '25 06:12

DeltaIV


1 Answers

If you really want to keep it in the polars family you can do

df.columns=pl.Series(df.columns).str.strip_chars()
like image 162
Dean MacGregor Avatar answered Dec 23 '25 20:12

Dean MacGregor