Login
📚 Python for Introductory Statistics
Chapters ▾

14.3 Descriptive Statistics (multiple columns)

Demo Vidoe Link

Sample Dataset

We will use the students test scores dataset found at my_csv_file.csv. See below on how to upload it. This is "clean" dataset.

  1. This is simulated data (not real)
  2. 180 rows (observations) and 8 columns (variables)
  3. time is completion time for test1_score
  4. id, gender, section and level are categorical data
  5. time, test1_score, test2_score, and test3_score are quantitative data

Basic Info about the Dataset

x_df.info gives basic information about the dataset stored in x_df: column names, non-empty data values, and data types.

Example: Write a program to display basic information about the dataset (URL below).

my_csv_file.csv

import pandas as pd

url = 'my_csv_file.csv'

x_df = pd.read_csv(url)
x_df.info()
Show expected output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 180 entries, 0 to 179
Data columns (total 8 columns):
 #   Column       Non-Null Count  Dtype  
---  ------       --------------  -----  
 0   id           180 non-null    int64  
 1   gender       180 non-null    object 
 2   section      179 non-null    object 
 3   time         180 non-null    float64
 4   level        180 non-null    object 
 5   test1_score  180 non-null    float64
 6   test2_score  180 non-null    float64
 7   test3_score  180 non-null    float64
dtypes: float64(4), int64(1), object(3)
memory usage: 11.4+ KB

Interpretation:

  1. The first row tells us the object x_df is of class DataFrame from pandas
  2. The second, RangeIndex, tells us there are 180 entries 0 to 179

Descriptive Stats: mean, std, quartiles, frequencies, correlations etc

Example: Write a program to read the dataset (URL below) and display the descriptive statistics - count, mean, std, 1st quartile, 2nd quartile, 3rd quartile and max of 'test1_score`.

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
x_df['test1_score'].describe()
Show expected output
count    180.000000
mean      74.410000
std        5.890378
min       58.800000
25%       70.700000
50%       74.150000
75%       78.625000
max       90.300000
Name: test1_score, dtype: float64

Example: Write a program to read the dataset (URL below) and display the descriptive statistics - count, mean, std, 1st quartile, 2nd quartile, 3rd quartile and max of test1_score and test2_score.

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
x_df[['test1_score','test2_score']].describe()
test1_scoretest2_score
count180.000000180.000000
mean74.41000086.150000
std5.8903782.545332
min58.80000078.500000
25%70.70000084.450000
50%74.15000086.650000
75%78.62500088.000000
max90.30000090.700000

Descriptive Statistics: All quantitative variables

Example: Write a program to read the dataset (URL below) and display the descriptive statistics - count, mean, std, 1st quartile, 2nd quartile, 3rd quartile and max of all variables(columns).

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
x_df.describe()   # summary for all quantitative columns 
idtimetest1_scoretest2_scoretest3_score
count180.000000180.000000180.000000180.000000180.000000
mean90.50000010.32111174.41000086.15000053.785000
std52.1056624.7253985.8903782.5453322.693101
min1.000000-4.50000058.80000078.50000049.900000
25%45.7500007.07500070.70000084.45000051.700000
50%90.5000009.95000074.15000086.65000053.200000
75%135.25000013.50000078.62500088.00000055.350000
max180.00000022.50000090.30000090.70000064.300000

The summary for the variable id is meaningless and should be ignored.

Grouped Frequencies

Example: Write a program to read the dataset (URL below) and display the frequency distribution by 'gender`. Change the frequencies to percent.

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
x_df.value_counts(subset=['gender'])  # value counts by the variable(column) gender
Show expected output
gender
male      98
female    82
dtype: int64
x_df.value_counts(subset=['gender'],normalize=True).mul(100).round(1).astype(str)+'%'
Show expected output
gender
male      54.4%
female    45.6%
dtype: object

Summary Statistics Grouped by Gender

Example: Write a program to read the dataset (URL below) and display a table of test1_score mean, min, max, std, size grouped by gender.

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
x_df.groupby(['gender']).agg({'test1_score': ['mean', 'min', 'max','std','size']})
test1_score
meanminmaxstdsize
gender
female74.01341558.887.66.23430982
male74.74183760.390.35.59712698

Example: Write a program to read the dataset (URL below) and display a table of test1_score mean, min, max, std, size grouped by level.

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
x_df.groupby(['level']).agg({'test1_score': ['mean', 'min', 'max','std','size']})
test1_score
meanminmaxstdsize
level
high79.61333370.990.33.61730660
low68.77500058.876.14.35028260
medium74.84166766.685.73.63888160

Example: Write a program to read the dataset (URL below) and display a table of test2_score mean, min, max, std, size grouped by section.

my_csv_file.csv

x_df.groupby(['section']).agg({'test1_score': ['mean', 'min', 'max','std','size']})
test1_score
meanminmaxstdsize
section
A73.86956558.887.66.36755246
B73.69583360.390.36.15539848
C73.44324361.784.26.34356837
D76.24166766.485.74.31296148

Grouped by Two Categorical Variables

Example: Write a program to read the dataset (URL below) and display a table of test1_score mean, min, max, std, size grouped by level and gender.

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
x_df.groupby(['level','gender']).agg({'test1_score': ['mean', 'min', 'max','std','size']})
test1_score
meanminmaxstdsize
levelgender
highfemale79.68400070.987.63.63772825
male79.56285771.690.33.65497935
lowfemale68.46969758.876.14.87637733
male69.14814860.376.13.66274127
mediumfemale75.72916771.481.42.80488424
male74.25000066.685.74.03127136

Grouped by Three Categorical Variables

Example: Write a program to read the dataset (URL below) and display a table of test1_score mean, min, max, std, size grouped by level, gender and section.

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
x_df.groupby(['level','section','gender']).agg({'test1_score': ['mean', 'min', 'max','std','size']})
test1_score
meanminmaxstdsize
levelsectiongender
highAfemale80.85714375.787.64.5952367
male78.57142972.986.14.3126617
Bfemale78.26666774.081.62.8380746
male80.17142973.690.35.3621257
Cfemale79.35000070.984.24.7492106
male79.47777871.683.13.6224229
Dfemale80.06666778.082.31.7095816
male79.70909175.284.22.27967311
lowAfemale67.78888958.874.15.5288899
male66.68333362.169.43.4913706
Bfemale66.23333360.572.14.5963756
male68.95000060.374.94.02140110
Cfemale66.90000061.774.04.2320219
male69.40000065.671.21.9252717
Dfemale72.21111166.476.13.2620729
male72.90000069.976.12.9709714
mediumAfemale76.36000071.881.33.8565535
male74.16666770.878.02.13300212
Bfemale76.02222272.681.43.1756019
male73.55000066.681.84.81854110
Cfemale75.65000075.675.70.0707112
male71.70000069.773.31.4899664
Dfemale75.02500071.477.22.2050278
male76.07000069.585.75.15321910

Correlation Coefficient (Pearson)

Example: Write a program to read the dataset (URL below) and display the correlation-coefficient between time and test1_score

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
# correlation coefficient between time and test1_score
x_df['time'].corr(x_df['test1_score'])
Show expected output
0.040726134954403075

Example: Write a program to read the dataset (URL below) and use x_df.corr to display the correlation-coefficient table between all columns.

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
x_df.corr()
idtimetest1_scoretest2_scoretest3_score
id1.0000000.0401440.7389100.0722360.024916
time0.0401441.0000000.0407260.061515-0.080249
test1_score0.7389100.0407261.0000000.0043670.030067
test2_score0.0722360.0615150.0043671.0000000.096539
test3_score0.024916-0.0802490.0300670.0965391.000000

Correlation Coefficient (Spearman)

Example: Write a program to read the dataset (URL below) and display the Spearman correlation-coefficient between test1_score and test1_score.

my_csv_file.csv

import pandas as pd

url='my_csv_file.csv'

x_df = pd.read_csv(url)
from scipy import stats
stats.spearmanr(x_df["test1_score"],x_df["test2_score"] )   # Spearman's rho - ranked no normality assumed
Show expected output
SpearmanrResult(correlation=0.008971805516759307, pvalue=0.9048529194438715)

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.