I’m working with a Polars DataFrame
, and I want to combine two columns into a dictionary format, where the values from one column become the keys and the values from the other column become the corresponding values.
Here’s an example DataFrame:
<code>import polars as pl
df = pl.DataFrame({
"name": ["Chuck", "John", "Alice"],
"surname": ["Dalliston", "Doe", "Smith"]
})
</code>
<code>import polars as pl
df = pl.DataFrame({
"name": ["Chuck", "John", "Alice"],
"surname": ["Dalliston", "Doe", "Smith"]
})
</code>
import polars as pl
df = pl.DataFrame({
"name": ["Chuck", "John", "Alice"],
"surname": ["Dalliston", "Doe", "Smith"]
})
I want to transform this DataFrame into a new column that contains dictionaries, where name is the key and surname is the value. The expected outcome should look like this:
<code>shape: (3, 3)
┌───────┬─────────┬──────────────────────────┐
│ name │ surname │ name_surname │
│ --- │ --- │ --- │
│ str │ str │ dict[str, str] │
├───────┼─────────┼──────────────────────────┤
│ Chuck │ Dalliston│ {"Chuck": "Dalliston"} │
│ John │ Doe │ {"John": "Doe"} │
│ Alice │ Smith │ {"Alice": "Smith"} │
└───────┴─────────┴──────────────────────────┘
</code>
<code>shape: (3, 3)
┌───────┬─────────┬──────────────────────────┐
│ name │ surname │ name_surname │
│ --- │ --- │ --- │
│ str │ str │ dict[str, str] │
├───────┼─────────┼──────────────────────────┤
│ Chuck │ Dalliston│ {"Chuck": "Dalliston"} │
│ John │ Doe │ {"John": "Doe"} │
│ Alice │ Smith │ {"Alice": "Smith"} │
└───────┴─────────┴──────────────────────────┘
</code>
shape: (3, 3)
┌───────┬─────────┬──────────────────────────┐
│ name │ surname │ name_surname │
│ --- │ --- │ --- │
│ str │ str │ dict[str, str] │
├───────┼─────────┼──────────────────────────┤
│ Chuck │ Dalliston│ {"Chuck": "Dalliston"} │
│ John │ Doe │ {"John": "Doe"} │
│ Alice │ Smith │ {"Alice": "Smith"} │
└───────┴─────────┴──────────────────────────┘
I’ve tried the following code:
<code>df.with_columns(
json = pl.struct("name", "surname").map_elements(json.dumps)
)
</code>
<code>df.with_columns(
json = pl.struct("name", "surname").map_elements(json.dumps)
)
</code>
df.with_columns(
json = pl.struct("name", "surname").map_elements(json.dumps)
)
But the result is not as expected. Instead of creating a dictionary with key-value
, it produces:
<code>{name:Chuck,surname:Dalliston}
</code>
<code>{name:Chuck,surname:Dalliston}
</code>
{name:Chuck,surname:Dalliston}