Pandas Tutorial Pandas References

Pandas DataFrame - kurtosis() function



The Pandas DataFrame kurtosis() function returns the unbiased kurtosis over the specified axis. The syntax for using this function is mentioned below:

Syntax

DataFrame.kurtosis(axis=None, skipna=None, 
                   level=None, numeric_only=None)

Parameters

axis Optional. Specify {0 or 'index', 1 or 'columns'}. If 0 or 'index', kurtosis are generated for each column. If 1 or 'columns', kurtosis are generated for each row. Default: 0
skipna Optional. Specify True to exclude NA/null values when computing the result. Default is True.
level Optional. Specify level (int or str). If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. A str specifies the level name.
numeric_only Optional. Specify True to include only float, int or boolean data. Default: False

Return Value

Returns kurtosis of Series or DataFrame if a level is specified.

Example: using kurtosis() column-wise on whole DataFrame

In the example below, a DataFrame df is created. The kurtosis() function is used to get the kurtosis for each column.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3),
  index = pd.date_range('1/1/2018', periods=5),
  columns = ['col_A', 'col_B', 'col_C']
)

print("The DataFrame is:")
print(df)

#kurtosis of all entries column-wise
print("\ndf.kurtosis() returns:")
print(df.kurtosis())

The output of the above code will be:

The DataFrame is:
               col_A     col_B     col_C
2018-01-01  0.343682  0.467754  0.731566
2018-01-02  0.588945  0.307980  0.397823
2018-01-03 -0.146330 -1.040161 -0.471874
2018-01-04 -0.732584  1.286889  0.022817
2018-01-05  0.422361 -0.613827  0.077464

df.kurt() returns:
col_A    0.194701
col_B   -1.141722
col_C    0.220942
dtype: float64

Example: using kurtosis() row-wise on whole DataFrame

To perform the operation row-wise, the axis parameter can be set to 1. Consider the example below.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 4),
  index = pd.date_range('1/1/2018', periods=5),
  columns = ['col_A', 'col_B', 'col_C', 'col_D']
)

print("The DataFrame is:")
print(df)

#kurtosis of all entries row-wise
print("\ndf.kurtosis(axis=1) returns:")
print(df.kurtosis(axis=1))

The output of the above code will be:

The DataFrame is:
               col_A     col_B     col_C     col_D
2018-01-01 -1.084664  0.845539 -0.435067  1.134583
2018-01-02 -1.717466  0.867263  0.427472 -0.721495
2018-01-03  1.054415  0.712278  0.149711  0.161991
2018-01-04 -0.147741  0.508162 -0.442463  0.004561
2018-01-05  2.680119  0.323035 -0.385693 -1.075277

df.kurt(axis=1) returns:
2018-01-01   -3.867439
2018-01-02   -2.201512
2018-01-03   -3.233237
2018-01-04    1.134319
2018-01-05    1.803169
Freq: D, dtype: float64

Example: using kurtosis() on selected column

Instead of whole DataFrame, the kurtosis() function can be applied on selected columns. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3),
  index = pd.date_range('1/1/2018', periods=5),
  columns = ['col_A', 'col_B', 'col_C']
)

print("The DataFrame is:")
print(df)

#kurtosis of single column
print("\ndf['col_B'].kurtosis() returns:")
print(df["col_B"].kurtosis())

#kurtosis of multiple columns
print("\ndf[['col_B', 'col_C']].kurtosis() returns:")
print(df[["col_B", "col_C"]].kurtosis())

The output of the above code will be:

The DataFrame is:
               col_A     col_B     col_C
2018-01-01 -1.050205 -1.226812  1.586253
2018-01-02  0.852263 -1.079776  1.125851
2018-01-03  1.302052 -0.253124  1.067236
2018-01-04 -0.979569 -1.181339 -0.815641
2018-01-05 -0.113263 -1.763929  0.053861

df['col_B'].kurtosis() returns:
2.1825483850743375

df[['col_B', 'col_C']].kurtosis() returns:
col_B    2.182548
col_C   -0.680592
dtype: float64

❮ Pandas DataFrame - Functions