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 !
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With