Transponieren Sie 2D -Array -Elemente in einer Spalte
Posted: 10 Apr 2025, 01:03
Ich habe einen Datenrahmen wie: < /p>
und ich möchte die 2D -Arrays in Spalte B auf Array transponieren [F32, (3, 800)] Stattdessen. Für den Fall, dass es einen saubereren Weg gibt.>
Code: Select all
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "numpy",
# "polars",
# ]
# ///
import numpy as np
import polars as pl
n_rows = 5
a = np.random.uniform(size=n_rows).astype(np.float32)
b = np.random.uniform(size=(n_rows, 800, 3)).astype(np.float32)
df = pl.DataFrame(
{
"a": a,
"b": b
},
schema={
"a": pl.Float32,
"b": pl.Array(pl.Float32, (800, 3))
}
)
print(df)
"""
shape: (5, 2)
┌──────────┬─────────────────────────────────┐
│ a ┆ b │
│ --- ┆ --- │
│ f32 ┆ array[f32, (800, 3)] │
╞══════════╪═════════════════════════════════╡
│ 0.667222 ┆ [[0.958246, 0.358944, 0.221115… │
│ 0.023049 ┆ [[0.514581, 0.48279, 0.998772]… │
│ 0.294279 ┆ [[0.10559, 0.017365, 0.236783]… │
│ 0.024168 ┆ [[0.487084, 0.438834, 0.589524… │
│ 0.259044 ┆ [[0.355924, 0.947524, 0.63777]… │
└──────────┴─────────────────────────────────┘
"""