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

MPA*_*MPA 4 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.
Run Code Online (Sandbox Code Playgroud)

如果我使用 pandas 它会像这样

df.Description.str.split(expand=True).stack().value_counts().reset_index()
Run Code Online (Sandbox Code Playgroud)

结果:

           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
Run Code Online (Sandbox Code Playgroud)

...

rit*_*e46 6

你可以这样做:

\n
csv = """\n0,                            Would never order again.\n1,   I\'m not sure it gives me any type of glow and ...\n2,   Goes on smoothly a bit sticky and color is clo...\n3,       Preferisco altri prodotti della stessa marca.\n4,        The moisturizing advertised is non-existent.\n""".encode()\n\n(pl.read_csv(csv, has_header=False, new_columns=["idx", "lines"])\n    .select(pl.col("lines").str.split(" ").flatten().alias("words"))\n    .groupby("words").agg(pl.count())\n    .sort("count", reverse=True)\n    .filter(pl.col("words").str.lengths() > 0)  \n)\n\n\n
Run Code Online (Sandbox Code Playgroud)\n

或者像这样:

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

两者输出:

\n
shape: (35, 2)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 words  \xe2\x94\x86 count \xe2\x94\x82\n\xe2\x94\x82 ---    \xe2\x94\x86 ---   \xe2\x94\x82\n\xe2\x94\x82 str    \xe2\x94\x86 u32   \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 is     \xe2\x94\x86 2     \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 and    \xe2\x94\x86 2     \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 order  \xe2\x94\x86 1     \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 it     \xe2\x94\x86 1     \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 ...    \xe2\x94\x86 ...   \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 Goes   \xe2\x94\x86 1     \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 The    \xe2\x94\x86 1     \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 stessa \xe2\x94\x86 1     \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 altri  \xe2\x94\x86 1     \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n\n
Run Code Online (Sandbox Code Playgroud)\n
\n