Skip to content

Equivalent of sum(case when-then-else-end) SQL statements in Ibis? #4657

Closed Answered by jcmkk3
lostmygithubaccount asked this question in Q&A
Discussion options

You must be logged in to vote

Most aggregates in ibis have a where parameter that you can use for conditional aggregation. You can also just sum a boolean comparison if you are trying to get a conditional count. Try something like this:

products.aggregate(
    products["product_price"].sum(where=product["is_drink_item"] == 1).name("subtotal_drink_price")
)

products.aggregate(
    (product["is_drink_item"] == 1).sum().name("drink_items_count")
)

There might be an even easier way, but that is how you'd translate it most directly.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@datamike
Comment options

Answer selected by lostmygithubaccount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants