英文:
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 ()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论