News for hackers. Stuff that matters.
Re:Lesser-known Pandas tricks (2019) by staticautomatic on Wednesday March 11, 2020 @02:53PM (#22549042)
My favorite, most elegant SO answer I've ever gotten was to a question about Pandas.

The question was "How do I create a column where each row's value is the mean of another column's values starting at that row?" The answer was:

  df.loc[::-1, 'col_1'].expanding().mean()[::-1]
Re:Lesser-known Pandas tricks (2019) by maest on Wednesday March 11, 2020 @04:35PM (#22550193)
I guess elegance is in the eye of the beholder.

Separately, I find it upsetting there are at least 6 ways of reversing a dataframe. It suggests some API smell.

Re:Lesser-known Pandas tricks (2019) by staticautomatic on Wednesday March 11, 2020 @04:51PM (#22550393)
I get tripped up by the Pandas API no matter how often I use it, and when I haven't used it in a while I practically have to re-learn all the basics. But whenever it does exactly what I need in a readable one-liner, I forgive it for all its transgressions. It's an abusive relationship.
Re:Lesser-known Pandas tricks (2019) by bobbylarrybobby on Wednesday March 11, 2020 @07:50PM (#22552058)
I dunno about elegant... seems like pandas is missing a reverse parameter in expanding. Surely this would be preferable:

df[col].expanding(reverse=True).mean()