Это лучший способ сделать это?
Код: Выделить всё
import polars as pl
# Sample DataFrame with multiple columns
df = pl.DataFrame({
"col1": [[1], [2], [3]], # list of int
"col2": [["a"], ["b"], ["c"]], # list of str
"col3": [[4], [5], [6]] # another list of int
})
# List of columns to convert
columns_to_convert = ["col1", "col2", "col3"]
# Apply the transformation to multiple columns
df = df.with_columns(
pl.col(col).list.first().alias(col) for col in columns_to_convert
)
print(df)
Код: Выделить всё
shape: (3, 3)
┌──────┬──────┬──────┐
│ col1 ┆ col2 ┆ col3 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞══════╪══════╪══════╡
│ 1 ┆ a ┆ 4 │
│ 2 ┆ b ┆ 5 │
│ 3 ┆ c ┆ 6 │
└──────┴──────┴──────┘
Подробнее здесь: https://stackoverflow.com/questions/785 ... -datatypes