По сути, у меня есть этот входной фрейм данных:
Код: Выделить всё
import polars as pl
pl.Config(fmt_table_cell_list_len=6, fmt_str_lengths=100)
df = pl.DataFrame({'a': [['the', 'dog', 'is', 'good', 'a'], ['toto', 'tata', 'I']]})
Код: Выделить всё
shape: (2, 1)
┌───────────────────────────────────┐
│ a │
│ --- │
│ list[str] │
╞═══════════════════════════════════╡
│ ["the", "dog", "is", "good", "a"] │
│ ["toto", "tata", "I"] │
└───────────────────────────────────┘
Код: Выделить всё
df.with_columns(
pl.col('a').list.eval(pl.element().value_counts())
)
Код: Выделить всё
shape: (2, 1)
┌───────────────────────────────────────────────────────┐
│ a │
│ --- │
│ list[struct[2]] │
╞═══════════════════════════════════════════════════════╡
│ [{"is",1}, {"the",1}, {"dog",1}, {"good",1}, {"a",1}] │
│ [{"tata",1}, {"I",1}, {"toto",1}] │
└───────────────────────────────────────────────────────┘
Код: Выделить всё
expected = pl.DataFrame({
"a": [
'{"the": 1, "dog": 1, "is": 1, "good": 1, "a": 1}',
'{"toto": 1, "tata": 1, "I": 1}'
]
})
Код: Выделить всё
shape: (2, 1)
┌──────────────────────────────────────────────────┐
│ a │
│ --- │
│ str │
╞══════════════════════════════════════════════════╡
│ {"the": 1, "dog": 1, "is": 1, "good": 1, "a": 1} │
│ {"toto": 1, "tata": 1, "I": 1} │
└──────────────────────────────────────────────────┘
Подробнее здесь: https://stackoverflow.com/questions/761 ... -into-a-js