Polars Transform String Containing Key-Values


Trying to figure out how to transform a k-v string that is inside a column where the k-v string is separated by commas, and could contain different keys. The different keys would then be transformed into their own columns, where missing values would contain nulls.

For example,

pl.DataFrame({    "apple": [1, 2, 3],    "data": ["a=b, b=c", "a=y, y=z", "k1=v1, k2=v2"]})

would look like:

pl.DataFrame({    "apple": [1, 2, 3],    "a": ["b", "y", None],    "b": ["c", None, None],    "y": [None, "z", None],    "k1": [None, None, "v1"],    "k2": [None, None, "v2"],    "data": ["a=b, b=c", "a=y, y=z", "k1=v1, k2=v2"]})

once transformed. Does anyone know what is the most efficient way to do this (perhaps without pre-processing of the data, if possible?)

enter image description here



Source link

Post a Comment

أحدث أقدم