[Python] python-polars split string column into many columns by delimiter

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    In pandas, the following code will split the string from col1 into many columns. is there a way to do this in polars?

    d = {"col1": ["a/b/c/d", "a/b/c/d"]}
    df= pd.DataFrame(data=d)
    df[["a","b","c","d"]]=df["col1"].str.split("/",expand=True)


    shape: (2, 5)
    ┌─────────┬─────┬─────┬─────┬─────┐
    │ col1 ┆ a ┆ b ┆ c ┆ d │
    │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
    │ str ┆ str ┆ str ┆ str ┆ str │
    ╞═════════╪═════╪═════╪═════╪═════╡
    │ a/b/c/d ┆ a ┆ b ┆ c ┆ d │
    │ a/b/c/d ┆ a ┆ b ┆ c ┆ d │
    └─────────┴─────┴─────┴─────┴─────┘

    Continue reading...

Compartilhe esta Página