Pandas agg define metric based on data type

Viewed 130

For pandas agg, is there a way to specify the aggregation function based on the data type? For example, all columns of type object get "first", all floats get "mean", and so on? So as to avoid having to type out all the columns with their respective aggregating functions.

Sample data:

import seaborn as sns
iris = sns.load_dataset('iris')

Desired code:

iris.agg({"object":"first", "float":"mean"})
3 Answers

I would do:

import seaborn as sns
iris = sns.load_dataset('iris')

agg_method = {'float64': 'mean', 'object':  'count'}

iris.agg({k: agg_method[str(v)] for k, v in iris.dtypes.items()})

Returns:

sepal_length      5.843333
sepal_width       3.057333
petal_length      3.758000
petal_width       1.199333
species         150.000000
dtype: float64
def a(x):
    if x.dtype == np.dtype('float64'):
        dict[x.name] = "mean"
    elif x.dtype == np.dtype('object'):
        dict[x.name] = "first"


dict = {}

df = df.apply(a)

iris.agg(dict)

An alternative, that does not rely on agg, is to apply the functions separately and concatenate:

pd.concat([iris.mean(numeric_only=True), 
           iris.select_dtypes('object').count()]
         )

sepal_length      5.843333
sepal_width       3.057333
petal_length      3.758000
petal_width       1.199333
species         150.000000
Related