Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are all Python Pandas .agg functions?

Tags:

python

pandas

I am talking about Python Pandas .agg() function, this one:

meanData = all_data.groupby(['Id'])[features].agg('mean')

So, it can do things like:

  • Mean
  • Median
  • Sum
  • Max
  • Min
  • Std

What else can it do? I found nothing on the official documentation page: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.DataFrameGroupBy.agg.html

like image 895
John Doe Avatar asked Dec 27 '18 10:12

John Doe


2 Answers

It can be just about any function that can be applied on a DataFrame object.

print(dir(DataFrame))

When func is a string type, the func name is looked up in the available attributes of the DataFrame object that the .agg method is invoked on.

https://github.com/pandas-dev/pandas/blob/v0.23.4/pandas/core/apply.py#L117

While it gives similar result when doing a division of elements in a DataFrame to write,

df = DataFrame([1,2,3,4])    
df.agg('true_div', 0, 2)

In the real world, you find that the method doing the operation is invoked in a direct manner on the DataFrame

df = DataFrame([1,2,3,4])
df.true_div(2)
like image 63
Oluwafemi Sule Avatar answered Oct 11 '22 05:10

Oluwafemi Sule


You can find the full list in documentation under pandas.core.groupby.GroupBy.some-function-name in the left menu.

Screen shot of where in documentation to find list

List currently includes many aggregation functions:

pipe, all, any, bfill, backfill, count, cumcount, cummax, cummin, cumprod, cumsum, ffill, first, head, last, max, mean, median, min, ngroup, nth, ohlc, pad, prod, rank, pct_change, size, sem, std, sum, var, tail

like image 25
Danny Varod Avatar answered Oct 11 '22 04:10

Danny Varod