Как получить value_counts для каждого слова в строковом столбце?Python

Программы на Python
Anonymous
 Как получить value_counts для каждого слова в строковом столбце?

Сообщение Anonymous »

У меня есть строковый столбец, и я хочу подсчитать количество слов во всем тексте.
Пример DataFrame:

Код: Выделить всё

import polars as pl

df = pl.DataFrame({
"Description": [
"Would never order again.",
"I'm not sure it gives me any type of glow and",
"Goes on smoothly a bit sticky and color is glow",
"Preferisco altri prodotti della stessa marca.",
"The moisturizing advertised is non-existent."
]
})
Если я использую pandas, я бы использовал .str.split, stack и value_counts

Код: Выделить всё

pl.from_pandas(
df.to_pandas().Description.str.split(expand=True)
.stack()
.value_counts()
.reset_index()
)

Код: Выделить всё

shape: (33, 2)
┌───────────────┬───────┐
│ index         ┆ count │
│ ---           ┆ ---   │
│ str           ┆ i64   │
╞═══════════════╪═══════╡
│ and           ┆ 2     │
│ glow          ┆ 2     │
│ is            ┆ 2     │
│ Would         ┆ 1     │
│ altri         ┆ 1     │
│ …             ┆ …     │
│ not           ┆ 1     │
│ I'm           ┆ 1     │
│ again.        ┆ 1     │
│ order         ┆ 1     │
│ non-existent. ┆ 1     │
└───────────────┴───────┘
Как бы я сделал это, используя только Polars?


Подробнее здесь: https://stackoverflow.com/questions/710 ... ing-column

Вернуться в «Python»