jaweej
jaweej

Reputation: 177

Is there a pandas equivalent of dplyr::summarise?

In R/dplyr, I can do

summarise(iris, max_width=max(Sepal.Width), min_width=min(Sepal.Width))

and get:

  max_width min_width
1       4.4         2

Is there something similar to summarise in pandas? I know describe(), but I would like the result to only contain a given summary statistic for a given column, not all summary statistics for all columns. In pandas, iris.describe() gives:

        sepal_length  sepal_width  petal_length  petal_width
count    150.000000   150.000000    150.000000   150.000000
mean       5.843333     3.057333      3.758000     1.199333
std        0.828066     0.435866      1.765298     0.762238
min        4.300000     2.000000      1.000000     0.100000
25%        5.100000     2.800000      1.600000     0.300000
50%        5.800000     3.000000      4.350000     1.300000
75%        6.400000     3.300000      5.100000     1.800000
max        7.900000     4.400000      6.900000     2.500000

Upvotes: 3

Views: 1622

Answers (2)

Panwen Wang
Panwen Wang

Reputation: 3825

To your question: Yes, there is.

>>> from datar.all import f, summarise, max, min
>>> from datar.datasets import iris
>>> 
>>> summarise(iris, max_width=max(f.Sepal_Width), min_width=min(f.Sepal_Width))
   max_width  min_width
   <float64>  <float64>
0        4.4        2.0

I am the author of the datar package.

Upvotes: 1

user2285236
user2285236

Reputation:

As of version 0.20, agg can be called on DataFrames too (source).

So you can do things like:

iris.agg({'sepal_width': 'min', 'petal_width': 'max'})

petal_width    2.5
sepal_width    2.0
dtype: float64

iris.agg({'sepal_width': ['min', 'median'], 'sepal_length': ['min', 'mean']})

        sepal_length  sepal_width
mean        5.843333          NaN
median           NaN          3.0
min         4.300000          2.0

Also see dplyr summarize equivalent in pandas. That one focuses on groupby operations though.

Upvotes: 2

Related Questions