如何获得类似于Polars中的`value_counts()`返回的结构数据类型?

huangapple go评论99阅读模式
英文:

How to get struct datatype similar to value_counts() return in polars?

问题

I am trying to combine two columns row wise and get something similar to what value_counts() returns in polars.

我正在尝试按行合并两列,并获得类似于value_counts()polars中返回的结果。

I have tried to use the to_struct but that didn't give the result that I expected.

我尝试使用to_struct,但它没有给出我预期的结果。

Expected Result:

预期结果:

│ struct[2] │
╞═══════════╡
│ {"c",4}   │
├───────────┤
│ {"a",3}   │
├───────────┤
│ {"b",1}   │
├───────────┤
│ {"d",1}   │
英文:

I am trying to combine two columns row wise and get something similar to what value_counts() returns in polars.

I have tried to use the to_struct but that didn't give the result that I expected.

data:

import polars as pl

df = pl.DataFrame({
    "tags": ["c", "a", "b", "d"],
    "vals":[4,3,1,1] 
})

df.to_struct
<bound method DataFrame.to_struct of shape: (4, 2)
┌──────┬──────┐
│ tags ┆ vals │
│ ---  ┆ ---  │
│ str  ┆ i64  │
╞══════╪══════╡
│ c    ┆ 4    │
│ a    ┆ 3    │
│ b    ┆ 1    │
│ d    ┆ 1    │

Expected Result:

│ struct[2] │
╞═══════════╡
│ {"c",4}   │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ {"a",3}   │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ {"b",1}   │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ {"d",1}   │

Not sure how can I do that as I have already tried to convert into struct datatype

答案1

得分: 1

to_struct是一个需要使用()调用的方法。

英文:

to_struct is a method that needs to be called with ().

huangapple
  • 本文由 发表于 2023年5月7日 01:04:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定