简体   繁体   中英

How to groupby and aggregate an operation to multiple columns?

I'm trying to create mean for rows in a data frame based on two columns, but I'm getting the following error:

TypeError: 'numpy.float64' object is not callable

The dataframe:

       date               origin  positive_score  neutral_score  negativity_score  compound_score
 2020-09-19            the verge           0.130          0.846             0.024          0.9833
 2020-09-19            the verge           0.130          0.846             0.024          0.9833
 2020-09-19                 fool           0.075          0.869             0.056          0.8560
 2020-09-19        seeking_alpha           0.067          0.918             0.015          0.9983
 2020-09-19        seeking_alpha           0.171          0.791             0.038          0.7506
 2020-09-19        seeking_alpha           0.095          0.814             0.091          0.9187
 2020-09-19        seeking_alpha           0.113          0.801             0.086          0.9890
 2020-09-19        seeking_alpha           0.094          0.869             0.038          0.9997
 2020-09-19  wall street journal           0.000          1.000             0.000          0.0000
 2020-09-19        seeking_alpha           0.179          0.779             0.042          0.9997
 2020-09-19        seeking_alpha           0.178          0.704             0.117          0.7360

My code:

    def mean_indicators(cls, df: pd.DataFrame):
        df_with_mean = df.groupby([DATE, ORIGIN], as_index=False).agg({POSITIVE_SCORE: df[POSITIVE_SCORE].mean(),
                                                                       NEGATIVE_SCORE: df[NEGATIVE_SCORE].mean(),
                                                                       NEUTRAL_SCORE: df[NEUTRAL_SCORE].mean(),
                                                                       COMPOUND_SCORE: df[COMPOUND_SCORE].mean()
                                                                       })
        return df_with_mean

I think this should do what you want:

def mean_indicators(cls, df: pd.DataFrame):
    df_with_mean = df.groupby([DATE, ORIGIN], as_index=False).agg(
    {POSITIVE_SCORE: "mean",
     NEGATIVE_SCORE: "mean",
     NEUTRAL_SCORE: "mean",
     COMPOUND_SCORE: "mean",
})
    return df_with_mean

You can alternatively use named aggregation syntax as seen here

# just groupby and mean
df_mean = df.groupby(['date', 'origin'], as_index=False).mean()

# display(df_mean())
       date               origin  positive_score  neutral_score  negativity_score  compound_score
 2020-09-19                 fool        0.075000       0.869000             0.056        0.856000
 2020-09-19        seeking_alpha        0.128143       0.810857             0.061        0.913143
 2020-09-19            the verge        0.130000       0.846000             0.024        0.983300
 2020-09-19  wall street journal        0.000000       1.000000             0.000        0.000000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM