Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kurtosis on groupby of pandas dataframe doesn't work

Tags:

python

pandas

When I apply the kurtosis function on a pandas datafame I always get following error:

AttributeError: Cannot access callable attribute 'kurt' of 'DataFrameGroupBy' objects, try using the 'apply' method

The following sample code works with all other statistical functions (mean(), skew(), ...), but not with kurtosis.

df = pd.DataFrame([[0,1,1,0,0,1],[0,1,2,4,5]]).T
df.columns = ['a','b']
df.groupby('a').kurt()

Any idea how I can apply kurtosis after groupby ? Thanks !

like image 406
Mark Avatar asked May 20 '16 11:05

Mark


People also ask

How do pandas find kurtosis?

The pandas DataFrame has a computing method kurtosis() which computes the kurtosis for a set of values across a specific axis (i.e., a row or a column). The pandas library function kurtosis() computes the Fisher's Kurtosis which is obtained by subtracting the Pearson's Kurtosis by three.

Is pandas Groupby lazy?

It is mentioned in this tutorial that pandas groupby object is lazy. it's lazy in nature. It doesn't really do any operations to produce a useful result until you say so.

What is As_index in Groupby pandas?

When as_index=True the key(s) you use in groupby() will become an index in the new dataframe. The benefits you get when you set the column as index are: Speed. When you filter values based on the index column eg. df.

How do you calculate Panda skewness?

Pandas DataFrame skew() Method The skew() method calculates the skew for each column. By specifying the column axis ( axis='columns' ), the skew() method searches column-wise and returns the skew of each row.


1 Answers

According to the API reference, kurt is not a method of the DataFrameGroupBy class, while mean and skew are.

This should work:

df.groupby('a').apply(pd.DataFrame.kurt)
like image 129
IanS Avatar answered Sep 20 '22 20:09

IanS