3 ms·
Hi Matt! Big fan. The first that I thought when I read this article was "How would Matt use his .assign-chain-kung-fu on this one" and I tried import pandas
by ivansavz 1y ago
Hi Matt! Big fan. The first that I thought when I read this article was "How would Matt use his .assign-chain-kung-fu on this one" and I tried
import pandas as pd
purchases = pd.read_csv("https://www.sumsar.net/blog/pandas-feels-clunky-when-coming-from-r/purchases.csv")
(purchases
.assign(country_median=lambda df:
df.groupby("country")["amount"].transform("median")
)
.query("amount <= country_median * 10")
.groupby("country")
.assign(total=lambda df: (df["amount"] - df["discount"]).sum())
)
but it seems 'DataFrameGroupBy' object has no attribute 'assign' so its not that simple,
though with a slight re-ordering of the chain operations, it works cf.
https://news.ycombinator.com/item?id=44236487 https://news.ycombinator.com/item?id=44236487