Get Variance in Python – List & pandas DataFrame Column (4 Examples)

This post explains how to calculate the variance in the Python programming language.

Example 1: Calculating the Variance of a List Object

my_lt = [10, 6, 2, 2, 15, 20, 3, 7, 4]      # Constructing a list in Python
print(my_lt)
# [10, 6, 2, 2, 15, 20, 3, 7, 4]
import numpy as np                          # Load NumPy library
print(np.var(my_lt))                        # Computing the variance of a list
# 34.888888888888886

Example 2: Calculating the Variance of the Columns in a pandas DataFrame

import pandas as pd                         # Import pandas library
my_df = pd.DataFrame({'A':range(10, 17),    # Constructing a pandas DataFrame
                      'B':[6, 1, 8, 5, 3, 8, 9],
                      'C':range(2, 9),
                      'GRP':['gr1', 'gr2', 'gr1', 'gr3', 'gr1', 'gr2', 'gr3']})
print(my_df)
#     A  B  C  GRP
# 0  10  6  2  gr1
# 1  11  1  3  gr2
# 2  12  8  4  gr1
# 3  13  5  5  gr3
# 4  14  3  6  gr1
# 5  15  8  7  gr2
# 6  16  9  8  gr3
print(my_df.var())                          # Computing the variance of all columns
# A    4.666667
# B    8.571429
# C    4.666667
# dtype: float64

Example 3: Calculating the Variance of the Columns in a pandas DataFrame by Group

print(my_df.groupby('GRP').var())           # Computing the column variances by group
#        A          B    C
# GRP                     
# gr1  4.0   6.333333  4.0
# gr2  8.0  24.500000  8.0
# gr3  4.5   8.000000  4.5

Example 4: Calculating the Variance of the Rows in a pandas DataFrame

print(my_df.var(axis = 1))                  # Computing the variance of all rows
# 0    16.000000
# 1    28.000000
# 2    16.000000
# 3    21.333333
# 4    32.333333
# 5    19.000000
# 6    19.000000
# dtype: float64

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top