Код: Выделить всё
list1 = [("a", 1), ("b", 2)]
list2 = [("c", 3), ("d", 4), ("e", 5)]
При печати фрейма данных я должен увидеть:
Код: Выделить всё
column_name
list[struct[2]]
[{"a",1}, {"b",2}]
[{"c",3}, {"d",4}, {"e",5}]
Код: Выделить всё
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
df = pl.DataFrame({
"col1": list1,
"col2": list2
})
print (df)
dfs = df.select(pl.struct(pl.all()).alias("my_struct"))
print(dfs)
РЕШЕНО:
Я решил эту проблему, используя следующий код. Похоже, в полярах структура имеет то же значение, что и dict в обычном Python.
Код: Выделить всё
list1 = [("a", 1), ("b", 2)]
list2 = [("c", 3), ("d", 4), ("e", 5)]
list_of_lists = [list1, list2]
lofl_as_structs = [[dict(f1=pair[0], f2=pair[1]) for pair in lst] for lst in list_of_lists]
df = pl.DataFrame({"column_name": lofl_as_structs})
print(df)
Код: Выделить всё
shape: (2, 1)
┌─────────────────────────────┐
│ column_name │
│ --- │
│ list[struct[2]] │
╞═════════════════════════════╡
│ [{"a",1}, {"b",2}] │
│ [{"c",3}, {"d",4}, {"e",5}] │
└─────────────────────────────┘
Я хотел бы иметь возможность сделать вышеописанное немного по-другому, указав следующую схему:
Код: Выделить всё
df = pl.DataFrame(lofl_as_structs,schema={'column_name': pl.List(pl.Struct([pl.Field('f1', pl.Utf8), pl.Field('f2', pl.Int64)]))})
Код: Выделить всё
raise ShapeError("the row data does not match the number of columns")
polars.exceptions.ShapeError: the row data does not match the number of columns
Подробнее здесь: https://stackoverflow.com/questions/775 ... f-type-lis
Мобильная версия