Код: Выделить всё
import polars as pl
df = pl.from_repr("""
┌───────────────┬──────────────┬───────────────┐
│ schema_name ┆ table_name ┆ column_name │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞═══════════════╪══════════════╪═══════════════╡
│ test_schema ┆ test_table ┆ test_column │
│ test_schema ┆ test_table ┆ test_column │
│ test_schema_2 ┆ test_table_2 ┆ test_column_2 │
└───────────────┴──────────────┴───────────────┘
""")
Код: Выделить всё
shape: (2, 3)
┌───────────────┬──────────────┬──────────────────────────┐
│ schema_name ┆ table_name ┆ column_name │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞═══════════════╪══════════════╪══════════════════════════╡
│ test_schema_2 ┆ test_table_2 ┆ test_column_2 │
│ test_schema ┆ test_table ┆ test_column, test_column │
└───────────────┴──────────────┴──────────────────────────┘
Код: Выделить всё
df.group_by('schema_name','table_name').agg(pl.col('column_name').alias('column_list'))
Код: Выделить всё
shape: (2, 3)
┌───────────────┬──────────────┬────────────────────────────────┐
│ schema_name ┆ table_name ┆ column_list │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ list[str] │
╞═══════════════╪══════════════╪════════════════════════════════╡
│ test_schema_2 ┆ test_table_2 ┆ ["test_column_2"] │
│ test_schema ┆ test_table ┆ ["test_column", "test_column"] │
└───────────────┴──────────────┴────────────────────────────────┘
Код: Выделить всё
column_listКод: Выделить всё
df['column_list_string'] = [','.join(map(str, l)) for l in df['column_list']]
Альтернативно, как мне сразу перейти от нескольких строк к одной строке, не используя список в качестве промежуточного шага?>
Подробнее здесь: https://stackoverflow.com/questions/751 ... to-one-row
Мобильная версия