I have a problem to merge columns into one. Say I have a dataframe (df) like below:
>> print(df)
shape: (3, 4)
┌─────┬───────┬───────┬───────┐
│ a ┆ b_a_1 ┆ b_a_2 ┆ b_a_3 │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str ┆ str │
╞═════╪═══════╪═══════╪═══════╡
│ 1 ┆ a-- ┆ ┆ │
├╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 1 ┆ ┆ b-- ┆ │
├╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 1 ┆ ┆ ┆ c-- │
└─────┴───────┴───────┴───────┘
And I want to be able to merge the last three (3) columsn into one using python-polars. I have tried and successfully got what I want. However,
>> out = df.select(pl.concat_str(['b_a_1', 'b_a_2', 'b_a_3']).alias('b_a'))
>> print(out)
shape: (3, 1)
┌─────┐
│ b_a │
│ --- │
│ str │
╞═════╡
│ a-- │
├╌╌╌╌╌┤
│ b-- │
├╌╌╌╌╌┤
│ c-- │
└─────┘
when I use regex in selecting the columns, I don't get the above result
>> out = df.select(pl.concat_str('^b_a_\d$'))
>> print(out)
shape: (3, 3)
┌───────┬───────┬───────┐
│ b_a_1 ┆ b_a_2 ┆ b_a_3 │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞═══════╪═══════╪═══════╡
│ a-- ┆ ┆ │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ ┆ b-- ┆ │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ ┆ ┆ c-- │
└───────┴───────┴───────┘
and nothing when run
>> out = df.select(pl.concat_str('^b_a_*$'))
>> print(out)
shape: (0, 0)
┌┐
╞╡
└┘
How am I to select the columns with regex and combine them into one?
Thank you very much for your time and suggestion.
Sincerely, Thi An