3 ms·
I wonder if the author would have thought Pandas feels less clunky if they knew about `.eval`? import pandas as pd purchases = pd.read_csv("purchases.
by ryan-duve 1y ago
I wonder if the author would have thought Pandas feels less clunky if they knew about `.eval`?
import pandas as pd
purchases = pd.read_csv("purchases.csv")
(
purchases.loc[
lambda x: x["amount"] < 10 * x.groupby("country")["amount"].transform("median")
]
.eval("total=amount-discount")
.groupby("country")["total"]
.sum()
)
- ivansavz 1y agoOr with and .assign: ( purchases.loc[ lambda x: x["amount"] < 10 * x.groupby("country")["amount"].transform("median") ] .assign(total=lambda df: df["amount"] - df["discount"]) .groupby("country")["total"] .sum() .reset_index() # to produce a DataFrame result )
- interiormut 1y ago``` library(data.table) purchases[amount <= median(amount)*10][, .(total = sum(amount - discount)), by = .(country)][order(country)] ``` - no quotes needed - no loc needed - only 1 groupby needed