2021年3月6日星期六

Count to first column and sum to the rest of the columns pandas groupby

I have a pandas DataFrame df with 290 columns.

Is there a way to make the .groupby operation concerning the following rules:

  1. sum operation for the 2st column.
  2. count operation to 3nd column.
  3. mean operation to all other columns

I know that I could use like this:

df.groupby("column1") \      .agg({"column2":"sum",             "column3":"count",            "column4":"mean",            ...            "column290":"mean"})  

But using this way is totally unproductive, since I have to type all the other columns.

Is there a way to set this operation? Like setting a default function when I don't set any to agg?

https://stackoverflow.com/questions/66511989/count-to-first-column-and-sum-to-the-rest-of-the-columns-pandas-groupby March 07, 2021 at 08:10AM

没有评论:

发表评论