Pandas Tutorial Pandas References

Pandas DataFrame - kurt() function



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

Syntax

DataFrame.kurt(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 kurt() column-wise on whole DataFrame

In the example below, a DataFrame df is created. The kurt() 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.kurt() returns:")
print(df.kurt())

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 kurt() 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.kurt(axis=1) returns:")
print(df.kurt(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 kurt() on selected column

Instead of whole DataFrame, the kurt() 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'].kurt() returns:")
print(df["col_B"].kurt())

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

The output of the above code will be:

The DataFrame is:
               col_A     col_B     col_C
2018-01-01  0.282922 -1.021156 -1.474771
2018-01-02 -1.490958 -0.104968  0.445577
2018-01-03  0.478941 -0.256432  0.629016
2018-01-04 -1.009856 -1.466656  0.097861
2018-01-05  1.880099  0.456219  2.352445

df['col_B'].kurt() returns:
-1.2954784722965753

df[['col_B', 'col_C']].kurt() returns:
col_B   -1.295478
col_C    1.624218
dtype: float64

❮ Pandas DataFrame - Functions