Variance by Group in Python (2 Examples) | pandas DataFrame Subgroups | Apply groupby & var Function

Published: 28 September 2022
on channel: Statistics Globe
196
6

How to find the variance by group in the Python programming language. More details: https://statisticsglobe.com/variance-...
Python code of this video:

import pandas as pd # Import pandas library to Python

data = pd.DataFrame({'x1':[5, 5, 2, 1, 2, 9, 2, 9, 7, 7, 8], # Create pandas DataFrame
'x2':range(32, 43),
'x3':range(20, 31),
'group1':['B', 'B', 'A', 'A', 'B', 'A', 'A', 'B', 'A', 'B', 'A'],
'group2':['a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b']})
print(data) # Print pandas DataFrame

print(data.groupby('group1').var()) # Get variance by group
x1 x2 x3
group1
A 12.566667 9.066667 9.066667
B 6.800000 14.700000 14.700000

print(data.groupby(['group1', 'group2']).var()) # Get variance by multiple groups
x1 x2 x3
group1 group2
A a 19.000000 2.333333 2.333333
b 10.333333 4.000000 4.000000
B a 3.000000 4.333333 4.333333
b 2.000000 2.000000 2.000000

Follow me on Social Media:
Facebook – Statistics Globe Page:   / statisticsglobecom  
Facebook – R Programming Group for Discussions & Questions:   / statisticsglobe  
Facebook – Python Programming Group for Discussions & Questions:   / statisticsglobepython  
LinkedIn – Statistics Globe Page:   / statisticsglobe  
LinkedIn – R Programming Group for Discussions & Questions:   / 12555223  
LinkedIn – Python Programming Group for Discussions & Questions:   / 12673534  
Twitter:   / joachimschork  

Music by bensound.com


On this page of the site you can watch the video online Variance by Group in Python (2 Examples) | pandas DataFrame Subgroups | Apply groupby & var Function with a duration of hours minute second in good quality, which was uploaded by the user Statistics Globe 28 September 2022, share the link with friends and acquaintances, this video has already been watched 196 times on youtube and it was liked by 6 viewers. Enjoy your viewing!