有没有办法将 value_counts polars 的结构输出列表获取到 json 中?

问题描述 投票:0回答:1

我从 Polars 开始,我试图对 polars 中的字符串列表进行字数统计,并将结果作为 dict 放入 polars 数据框中。

基本上,我有这个输入数据框:screenshot

df_test = pl.DataFrame({'a': [['the', 'dog', 'is', 'good', 'a'], ['toto', 'tata', 'I']]})
shape: (2, 1)
┌───────────────────────┐
│ a                     │
│ ---                   │
│ list[str]             │
╞═══════════════════════╡
│ ["the", "dog", … "a"] │
│ ["toto", "tata", "I"] │
└───────────────────────┘

我得到这个输出: screenshot

shape: (2, 1)
┌───────────────────────────────────┐
│ a                                 │
│ ---                               │
│ list[struct[2]]                   │
╞═══════════════════════════════════╡
│ [{"is",1}, {"dog",1}, … {"good",… │
│ [{"tata",1}, {"I",1}, {"toto",1}… │
└───────────────────────────────────┘

来自那个命令:

df_test.with_columns(
    pl.col('a').arr.eval(pl.element().value_counts())
)

有没有办法得到这样的 value_counts 的结果? enter image description here

提前致谢

json dataframe python-polars word-count
1个回答
0
投票

您可以将

value_counts
输出转换为一个字符串,对每个字符串进行一些操作(删除所有
{}
,将
,
替换为
:
),
arr.join
列表变成一个大字符串,然后添加回来最后
{}
pl.format

df_test.with_columns(
        a=pl.format(
            "{{}}",
            pl.col("a")
            .arr.eval(
                pl.element()
                .value_counts()
                .cast(str)
                .str.strip("{}")
                .str.replace(",", ": ")
            )
            .arr.join(", "),
        )
    )
shape: (2, 1)
┌──────────────────────────────────────────────────┐
│ a                                                │
│ ---                                              │
│ str                                              │
╞══════════════════════════════════════════════════╡
│ {"good": 1, "dog": 1, "a": 1, "is": 1, "the": 1} │
│ {"tata": 1, "toto": 1, "I": 1}                   │
└──────────────────────────────────────────────────┘

只要没有单词有

,
,这就有效,这对于单词计数来说似乎是一个安全的假设。我认为在
pl.format
中使用
arr.eval
可能会有更一般的答案,但是当我尝试在该上下文中使用
struct
表达式作为
pl.format
的参数时,我遇到了错误。

© www.soinside.com 2019 - 2024. All rights reserved.