如何将这段代码更改为 Polars?” TypeError: ‘GroupBy’ 对象不可订阅”

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

How to change this code to polars ?" TypeError: 'GroupBy' object is not subscriptable"

问题

This code is pandas.

pandas_reserve_tb \
        .groupby(['hotel_id', 'people_num'])['total_price'] \
        .sum().reset_index()

I would like to change this code to polars.

polars_reserve_tb \
        .groupby("hotel_id", "people_num")['total_price'] \
        .sum().with_row_count()

But, I got the error:

"TypeError: 'GroupBy' object is not subscriptable"

How to solve this error?

英文:

This code is pandas.

pandas_reserve_tb \
        .groupby(['hotel_id', 'people_num'])['total_price'] \
        .sum().reset_index()

I would like to change this code to polars.

polars_researve_tb \
        .groupby("hotel_id", "people_num")['total_price'] \
        .sum().with_row_count()

But, I got the error

> "TypeError: 'GroupBy' object is not subscriptable"

How to solove this error?

答案1

得分: 6

你可能是指

polars_researve_tb \
        .groupby(["hotel_id", "people_num"]).agg(pl.col('total_price').sum())

我建议将可重现的示例发布在将来。

英文:

You probably meant

polars_researve_tb \
        .groupby(["hotel_id", "people_num"]).agg(pl.col('total_price').sum())

I'd advise posting reproducible examples in the future

huangapple
  • 本文由 发表于 2023年2月19日 21:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500367.html
匿名

发表评论

匿名网友

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

确定