How could I split a column of string into list of list?
Minimum example:
import polars as pl
pl.Config(fmt_table_cell_list_len=6, fmt_str_lengths=100)
df = pl.DataFrame({'test': "A,B,C,1\nD,E,F,2\nG,H,I,3\nJ,K,L,4"})
shape: (1, 1)
┌─────────┐
│ test │
│ --- │
│ str │
╞═════════╡
│ A,B,C,1 │
│ D,E,F,2 │
│ G,H,I,3 │
│ J,K,L,4 │
└─────────┘
I try the following, somehow I stop after the first split
df = df.with_columns(pl.col('test').str.split('\n'))
My desired result would be it return a list of list inside the dataframe, so that the list of list is readily to be read by other columns
result = pl.DataFrame({'test': [[["A","B","C",1], ["D","E","F",2], ["G","H","I",3], ["J","K","L",4]]]}, strict=False)
result = result.with_columns(
get_data = pl.col('test').list[2].list[3].cast(pl.Int64) # Answer = 3
)
shape: (1, 2)
┌──────────────────────────────────────────────────────────────────────────────────────────┬──────────┐
│ test ┆ get_data │
│ --- ┆ --- │
│ list[list[str]] ┆ i64 │
╞══════════════════════════════════════════════════════════════════════════════════════════╪══════════╡
│ [["A", "B", "C", "1"], ["D", "E", "F", "2"], ["G", "H", "I", "3"], ["J", "K", "L", "4"]] ┆ 3 │
└──────────────────────────────────────────────────────────────────────────────────────────┴──────────┘