Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Polars convert string of digits to list

So i have a polars column/series that is strings of digits.

s = pl.Series("a", ["111","123","101"])
s
shape: (3,)
Series: 'a' [str]
[
    "111"
    "123"
    "101"
]

I would like to convert each string into a list of integers.
I have found a working solution but i am not sure if it is optimal.

s.str.split("").arr.shift(1).arr.slice(2).arr.eval(pl.element().str.parse_int(10))
shape: (3,)
Series: 'a' [list[i32]]
[
    [1, 1, 1]
    [1, 2, 3]
    [1, 0, 1]
]

I first split the strings at each point. For the first row this gives me ["","1","1","1",""]. From this i want to remove the first and last entries (the empty string). Since i dont know the length of the entries beforehand and slice doesnt let one specify an end index i went with the shift -> slice version but i feel that there has to be a better way.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Lastly is the application of the parse_int.

This seems to be working but id like to know if there are better ways to do this or any of the individual steps.

>Solution :

.extract_all() and .cast()

s.str.extract_all(r"\d").cast(pl.List(pl.Int64))
shape: (3,)
Series: 'a' [list[i64]]
[
    [1, 1, 1]
    [1, 2, 3]
    [1, 0, 1]
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading