Pandas Tutorial Pandas References

Pandas Series - kurtosis() function



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

Syntax

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

Parameters

axis Optional. Specify {0 or 'index'}. Specify axis for the function to be applied on.
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 scalar. A str specifies the level name.
numeric_only Optional. Specify True to include only float, int or boolean data. Default: False

Return Value

Returns scalar or Series if a level is specified.

Example: using kurtosis() on a Series

In the example below, the kurtosis() function is used to get the kurtosis of a given series.

import pandas as pd
import numpy as np

idx = pd.MultiIndex.from_arrays([
  ['rand', 'rand', 'rand', 'rand', 
   'randn', 'randn', 'randn', 'randn']],
    names=['DataType'])

x = pd.Series(np.append(np.random.rand(4), 
              np.random.randn(4)), index=idx)

print("The Series contains:")
print(x)

#kurtosis of all values in the series
print("\nx.kurtosis() returns:")
print(x.kurtosis())

#kurtosis of all values within given level
print("\nx.kurtosis(level='DataType') returns:")
print(x.kurtosis(level='DataType'))
print("\nx.kurtosis(level=0) returns:")
print(x.kurtosis(level=0))

The output of the above code will be:

The Series contains:
DataType
rand        0.132188
rand        0.250332
rand        0.205490
rand        0.105652
randn       0.984456
randn      -0.058915
randn       0.776789
randn       0.345381
dtype: float64

x.kurtosis() returns:
0.12693202361906497

x.kurtosis(level='DataType') returns:
DataType
rand    -3.220555
randn   -1.910558
dtype: float64

x.kurtosis(level=0) returns:
DataType
rand    -3.220555
randn   -1.910558
dtype: float64

Example: using kurtosis() on selected series in a DataFrame

Similarly, the kurtosis() function can be applied on selected series/column of a given DataFrame. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Last Salary": [58, 60, 63, 57],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

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

#kurtosis of all values of 'Salary' series
print("\ndf['Salary'].kurtosis() returns:")
print(df["Salary"].kurtosis())

The output of the above code will be:

The DataFrame is:
       Bonus  Last Salary  Salary
John       5           58      60
Marry      3           60      62
Sam        2           63      65
Jo         4           57      59

df['Salary'].kurtosis() returns:
-0.2857142857142865

❮ Pandas Series - Functions

5