I have a DataFrame as follows:
shop | item_price | item_sold
A | 123 | 1
B | 921 | 2
A | 28 | 4
...
I want to find the total revenue by each shop. In SQL it looks like this:
SELECT shop, SUM((item_price * item_sold)) as revenue
FROM table
GROUP BY shop
I want to do it in Python using Pandas. I tried:
revenue_by_shop = table.groupby("shop")[table["item_price"] * table["item_sold"]].sum()
But that does not seem like the right answer.
df.assign(revenue=df.item_price * df.item_sold).groupby("shop").revenue.sum()