Variance in Python (5 Examples) | List, pandas DataFrame Column & Row | How to Calculate by Group

preview_player
Показать описание
Python code of this video:

my_list = [1, 5, 3, 9, 5, 8, 3, 1, 1] # Create example list
print(my_list) # Print example list
# [1, 5, 3, 9, 5, 8, 3, 1, 1]

import numpy as np # Load NumPy

# 8.0

import pandas as pd # Import pandas library in Python

data = pd.DataFrame({'x1':range(32, 1, - 2), # Create pandas DataFrame
'x2':[5, 2, 7, 3, 1, 4, 3, 4, 4, 2, 3, 3, 1, 1, 7, 5],
'x3':range(100, 116),
'group':['A', 'A', 'B', 'C', 'B', 'C', 'C', 'A', 'C', 'A', 'C', 'A', 'B', 'B', 'B', 'A']})
print(data) # Print pandas DataFrame

print(data['x1'].var()) # Get variance of one column
# 90.66666666666667

# x1 90.666667
# x2 3.595833
# x3 22.666667
# dtype: float64

# 0 2396.333333
# 1 2604.333333
# 2 2490.333333
# 3 2743.000000
# 4 2923.000000
# 5 2902.333333
# 6 3049.000000
# 7 3121.000000
# 8 3237.333333
# 9 3436.333333
# 10 3522.333333
# 11 3652.333333
# 12 3864.333333
# 13 4003.000000
# 14 3926.333333
# 15 4146.333333
# dtype: float64

# x1 x2 x3
# group
# A 135.066667 1.9 33.766667
# B 124.000000 10.8 31.000000
# C 29.200000 0.3 7.300000

Follow me on Social Media:

Рекомендации по теме