# Per store revenue for high price items
df_agg = (
df1
.query("unit_price > 400")
.groupby('store_id','store_name')
.agg({'revenue': np.sum,
'customer_id': 'nunique'})
)Groupby->agg is a good one to chain because you don’t want the intermediary almost ever.
Edit: also, I never understand people who use query. That just seems like it’s begging for problems later on. .loc for life.
Though if you replaced each subsequent line with df1 and df2 and so on he wouldn’t mind as much.
I can’t opine as to whether one approach or the other is intrinsically better. But echoes of his tirades still ring when I see the same variable name redefined.
In this case I would not think re-assigning df as being in violation those principles. Might even be useful if it plays better with how the code interacts with debuggers and version control.
Its not really re-used in the sense that would motivate making up new names for the intermediate steps. It’s clearly just used as a syntactic aid for the operation chaining. So in my mind both expressions are equivalent from that perspective.
I would probably insist on limiting its scope to precisely that expression though, to maintain that obviousness.
( df.pipe(went_up, 'hill')
)is much better then something like that
df = went_up(df, 'hill')
df = fetch(df, 'water')
df = fell_down(df, 'jack')
df = broke(df, 'jack')
df = tumble_after(df, 'jill')
Would really like to hear an opinion about that.