python-polars - Python - Polars - 字符串列上的值计数

标签 python-polars

如何在 Polars DataFrame 上应用字数统计 我有一个字符串列,我想对所有文本进行字数统计。 谢谢

数据框示例:

0                             Would never order again.
1    I'm not sure it gives me any type of glow and ...
2    Goes on smoothly a bit sticky and color is clo...
3        Preferisco altri prodotti della stessa marca.
4         The moisturizing advertised is non-existent.

如果我使用 pandas,它是这样的

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

结果:

           index  0
0             the  2
1             and  2
2           brown  2
3              is  2
4             any  1
5             The  1
6    moisturizing  1
7            like  1
8             I'm  1
9             not  1
10         closer  1
11         stessa  1
12       prodotti  1
13  non-existent.  1
14     advertised  1
15              I  1
16             of  1
17          order  1

...

最佳答案

你可以这样做:

csv = """
0,                            Would never order again.
1,   I'm not sure it gives me any type of glow and ...
2,   Goes on smoothly a bit sticky and color is clo...
3,       Preferisco altri prodotti della stessa marca.
4,        The moisturizing advertised is non-existent.
""".encode()

(pl.read_csv(csv, has_header=False, new_columns=["idx", "lines"])
    .select(pl.col("lines").str.split(" ").flatten().alias("words"))
    .groupby("words").agg(pl.count())
    .sort("count", reverse=True)
    .filter(pl.col("words").str.lengths() > 0)  
)


或者像这样:

(pl.read_csv(csv, has_header=False, new_columns=["idx", "lines"])
    .select(pl.col("lines").str.split(" ").flatten().alias("words"))
    .to_series()
    .value_counts()
    .filter(pl.col("words").str.lengths() > 0)  
)

两个输出:

shape: (35, 2)
┌────────┬───────┐
│ words  ┆ count │
│ ---    ┆ ---   │
│ str    ┆ u32   │
╞════════╪═══════╡
│ is     ┆ 2     │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ and    ┆ 2     │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ order  ┆ 1     │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ it     ┆ 1     │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ ...    ┆ ...   │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ Goes   ┆ 1     │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ The    ┆ 1     │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ stessa ┆ 1     │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ altri  ┆ 1     │
└────────┴───────┘

关于python-polars - Python - Polars - 字符串列上的值计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71015311/

相关文章:

python - Polars DataFrame 过滤一段时间内的数据(开始时间和结束时间)

python - Polars - 用其他列的值替换列中的部分字符串

python - 使用另一列中的值对 str.starts_with() 进行极坐标分析

pandas - Polars 将性能应用于自定义功能

python - 将包含当前日期和时间的列添加到 Polars DataFrame

python - 如何将 Poisson CDF 编写为 Python Polars 表达式

python - 在条件连接 + groupby/agg 上下文中,Polars 比 DuckDB 慢得多

python - Polar 日期 YYYY 周

python - Polars - 将列中少于 X 个唯一值的值替换为不同的特定值

python-polars - 如何将 .apply 的结果添加为极坐标中的新列?