Closed AAG-AZTEC closed 7 months ago
I am too keep getting the same error while using this line ----> products.groupby(["Brand"]).mean()
This behavior was changed in pandas 2.0: https://pandas.pydata.org/docs/whatsnew/v1.5.0.html#numeric-only-default-value
Specify .mean(numeric_only=True)
if you'd like to keep the old behavior.
Use this it will work fine
result = df.groupby(grouping_columns).mean(numeric_only=True)
what if you're using .agg() function to perform multiple operations on the grouped columns.
eg : movies.groupby(['Director', 'Star1']).agg(['min', 'max', 'mean'])
It shows the same error (TypeError: agg function failed [how->mean,dtype->object])
Even after I used the numeric_only = True parameter, it returned
TypeError: Cannot use numeric_only=True with SeriesGroupBy.min and non-numeric dtypes.
Please help
what if you're using .agg() function to perform multiple operations on the grouped columns.
eg : movies.groupby(['Director', 'Star1']).agg(['min', 'max', 'mean'])
It shows the same error (TypeError: agg function failed [how->mean,dtype->object])
Even after I used the numeric_only = True parameter, it returned
TypeError: Cannot use numeric_only=True with SeriesGroupBy.min and non-numeric dtypes.
Please help
you are trying to use the min() method on a data group that contains non-numeric (atypical) data types you can use the .select_type() method to select only numeric columns before performing the addition operation.
df.select_types(include='number')
the .select_types(include='number') method selects only numeric columns from the Data Frame df
not working for me.
@sarwesh123 This works like magic. Thank you so much
I"m using pandas version 2.2.2 still its not working
Pandas version checks
[ ] I have checked that this issue has not already been reported.
[ ] I have confirmed this bug exists on the latest version of pandas.
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
The .groupby is not excluding the strings(objects) in this dataframe. In the older versions (1.3.5 pandas), this works for mean, min, max and even aggregate function.
Expected Behavior
agg function failed [how->mean,dtype->object]
Installed Versions