by cmdlineluser · 2y ago · 0 comments
In Python, I have been finding Polars nicer to use:
(purchases
.filter(pl.col("amount") <= pl.col("amount").median().over("country") * 10)
.group_by("country")
.agg(total = (pl.col("amount") - pl.col("discount")).sum())
)
Not as compact as the R example but gets a bit closer compared to the pandas approach. SELECT country, SUM(amount - discount) AS total
FROM purchases
WHERE amount <= (
SELECT MEDIAN(amount) * 10
FROM purchases
WHERE country = purchases.country
)
GROUP BY country;
It might be just an issue of familiarity but sql seems the most straightforward and easy to understand for me.Using an appropriate DSL for the problem may be useful. In Python:
df.to_sql('purchases', db, index=False)
print(*db.execute(query))
# -> ('Canada', 270) ('USA', 8455)
e.g., we can use regexes to query text. Python is a general-purpose language, you can query text without using regexes but it would be insanity to ignore regexes completely (I don't know how easy is to invoke regexes from R). Another example, bash pipeline can be embedded in Python ("generate --flag | filter arg | sink") without reimplementing it in pure Python (you can do it but it would be ugly). No idea how easy it is to invoke shell commands from R. SQL is just another DSL in this case -- use it in Python when it makes the solution more readable. SELECT p.country, SUM(p.amount - p.discount) AS total
FROM purchases p
JOIN (
SELECT country, MEDIAN(amount) * 10 AS median_amount
FROM purchases
GROUP BY country
) m ON p.country = m.country
WHERE p.amount <= m.median_amount
GROUP BY p.country;Python as a glue language is one of its strong sides.