Facebook Page Twitter Page LinkedIn Page
× Pandas Tutorial Pandas Resources


The Pandas Series - skew() function is used to return the unbiased skew over the specified axis. The syntax for using this function is mentioned below:

Syntax

Series.skew(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 skew() on a Series

In the below example, the skew() function is used to get the skew 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)

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

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

The output of the above code will be:

The Series contains:
DataType
rand     0.928475
rand     0.577150
rand     0.617906
rand     0.919045
randn   -0.562844
randn    0.298008
randn   -1.564290
randn   -1.268517
dtype: float64

x.skew() returns:
-0.756782207841

x.skew(level='DataType') returns:
DataType
randn    0.735865
rand    -0.037910
dtype: float64

x.skew(level=0) returns:
DataType
randn    0.735865
rand    -0.037910
dtype: float64

Example: Using skew() on selected series in a DataFrame

Similarly, the skew() 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)

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

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'].skew() returns:
0.86391879545

❮ Pandas Series - Functions