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.
If you really want to keep it in the polars family you can do
df.columns=pl.Series(df.columns).str.strip_chars()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With