Skip to content
Advertisement

python-polars horizontally stack dataframe columns

how to horizontally stack data frame columns. the code below shows two data frames, I want to stack col 2 from df2 with col 1 from df1. how can I do this?

import polars as pl
df1 = pl.DataFrame(
    {
        "col1": ["a", "b", "c", "d"]    }
)


df2 = pl.DataFrame(
    {
        "col2": ["1", "2", "3", "4"]
    }
)

Advertisement

Answer

df1 = pl.DataFrame(
    {
        "col1": ["a", "b", "c", "d"]    }
)


df2 = pl.DataFrame(
    {
        "col2": ["1", "2", "3", "4"]
    }
)

pl.concat([df1, df2], how="horizontal")
shape: (4, 2)
┌──────┬──────┐
│ col1 ┆ col2 │
│ ---  ┆ ---  │
│ str  ┆ str  │
╞══════╪══════╡
│ a    ┆ 1    │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ b    ┆ 2    │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ c    ┆ 3    │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ d    ┆ 4    │
└──────┴──────┘

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement