5 ms·
I looked at your code very quickly, but it looks like you need to use .filter after a .groupby...
by __mharrison__ 5d ago
I looked at your code very quickly, but it looks like you need to use .filter after a .groupby...
- latent-person 5d agoThe text right above the code says why you can't... edit: Let me clarify. From the blog-post: > since a `DataFrameGroupBy` object doesn’t have a `.query()` or boolean-indexing shortcut of its own, so filtering within groups needs `.apply()` again, and the surrounding pipeline has to be rebuilt around it: Hence you really do need one of the versions of the code I gave. You can't do the naive approach with just `.groupby().filter(lambda: )`, since you need a row-wise decision.
- __mharrison__ 5d agoI'm confused, you can use filter after a groupby in pandas... It's late here, I'm going to bed, perhaps I'll write the code tomorrow when I'm at my laptop and not on my phone.
- deleted 5d ago[deleted]
- __mharrison__ 4d agoI misspoke, you need to use .groupby/.transform to add a new filtering column: (sales .assign(country_median=lambda df_: ( df_.groupby("country")["amount"].transform("median") )) .query("amount <= country_median * 10") .assign(net=pd.col('amount') - pd.col('discount')) .groupby("country", as_index=False) .agg(total=("net", "sum")) )
- latent-person 4d agoYes, so basically equivalent to the code I showed in the blog.