14.3 Descriptive Statistics (multiple columns)
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.
- This is simulated data (not real)
- 180 rows (observations) and 8 columns (variables)
timeis completion time fortest1_scoreid, gender, section and levelare categorical datatime, test1_score, test2_score, and test3_scoreare 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+ KBInterpretation:
- The first row tells us the object
x_dfis ofclassDataFrame from pandas - 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: float64Example: 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_score | test2_score | |
|---|---|---|
| count | 180.000000 | 180.000000 |
| mean | 74.410000 | 86.150000 |
| std | 5.890378 | 2.545332 |
| min | 58.800000 | 78.500000 |
| 25% | 70.700000 | 84.450000 |
| 50% | 74.150000 | 86.650000 |
| 75% | 78.625000 | 88.000000 |
| max | 90.300000 | 90.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
| id | time | test1_score | test2_score | test3_score | |
|---|---|---|---|---|---|
| count | 180.000000 | 180.000000 | 180.000000 | 180.000000 | 180.000000 |
| mean | 90.500000 | 10.321111 | 74.410000 | 86.150000 | 53.785000 |
| std | 52.105662 | 4.725398 | 5.890378 | 2.545332 | 2.693101 |
| min | 1.000000 | -4.500000 | 58.800000 | 78.500000 | 49.900000 |
| 25% | 45.750000 | 7.075000 | 70.700000 | 84.450000 | 51.700000 |
| 50% | 90.500000 | 9.950000 | 74.150000 | 86.650000 | 53.200000 |
| 75% | 135.250000 | 13.500000 | 78.625000 | 88.000000 | 55.350000 |
| max | 180.000000 | 22.500000 | 90.300000 | 90.700000 | 64.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: int64x_df.value_counts(subset=['gender'],normalize=True).mul(100).round(1).astype(str)+'%'
Show expected output
gender
male 54.4%
female 45.6%
dtype: objectSummary 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 | |||||
|---|---|---|---|---|---|
| mean | min | max | std | size | |
| gender | |||||
| female | 74.013415 | 58.8 | 87.6 | 6.234309 | 82 |
| male | 74.741837 | 60.3 | 90.3 | 5.597126 | 98 |
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 | |||||
|---|---|---|---|---|---|
| mean | min | max | std | size | |
| level | |||||
| high | 79.613333 | 70.9 | 90.3 | 3.617306 | 60 |
| low | 68.775000 | 58.8 | 76.1 | 4.350282 | 60 |
| medium | 74.841667 | 66.6 | 85.7 | 3.638881 | 60 |
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 | |||||
|---|---|---|---|---|---|
| mean | min | max | std | size | |
| section | |||||
| A | 73.869565 | 58.8 | 87.6 | 6.367552 | 46 |
| B | 73.695833 | 60.3 | 90.3 | 6.155398 | 48 |
| C | 73.443243 | 61.7 | 84.2 | 6.343568 | 37 |
| D | 76.241667 | 66.4 | 85.7 | 4.312961 | 48 |
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 | ||||||
|---|---|---|---|---|---|---|
| mean | min | max | std | size | ||
| level | gender | |||||
| high | female | 79.684000 | 70.9 | 87.6 | 3.637728 | 25 |
| male | 79.562857 | 71.6 | 90.3 | 3.654979 | 35 | |
| low | female | 68.469697 | 58.8 | 76.1 | 4.876377 | 33 |
| male | 69.148148 | 60.3 | 76.1 | 3.662741 | 27 | |
| medium | female | 75.729167 | 71.4 | 81.4 | 2.804884 | 24 |
| male | 74.250000 | 66.6 | 85.7 | 4.031271 | 36 |
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 | |||||||
|---|---|---|---|---|---|---|---|
| mean | min | max | std | size | |||
| level | section | gender | |||||
| high | A | female | 80.857143 | 75.7 | 87.6 | 4.595236 | 7 |
| male | 78.571429 | 72.9 | 86.1 | 4.312661 | 7 | ||
| B | female | 78.266667 | 74.0 | 81.6 | 2.838074 | 6 | |
| male | 80.171429 | 73.6 | 90.3 | 5.362125 | 7 | ||
| C | female | 79.350000 | 70.9 | 84.2 | 4.749210 | 6 | |
| male | 79.477778 | 71.6 | 83.1 | 3.622422 | 9 | ||
| D | female | 80.066667 | 78.0 | 82.3 | 1.709581 | 6 | |
| male | 79.709091 | 75.2 | 84.2 | 2.279673 | 11 | ||
| low | A | female | 67.788889 | 58.8 | 74.1 | 5.528889 | 9 |
| male | 66.683333 | 62.1 | 69.4 | 3.491370 | 6 | ||
| B | female | 66.233333 | 60.5 | 72.1 | 4.596375 | 6 | |
| male | 68.950000 | 60.3 | 74.9 | 4.021401 | 10 | ||
| C | female | 66.900000 | 61.7 | 74.0 | 4.232021 | 9 | |
| male | 69.400000 | 65.6 | 71.2 | 1.925271 | 7 | ||
| D | female | 72.211111 | 66.4 | 76.1 | 3.262072 | 9 | |
| male | 72.900000 | 69.9 | 76.1 | 2.970971 | 4 | ||
| medium | A | female | 76.360000 | 71.8 | 81.3 | 3.856553 | 5 |
| male | 74.166667 | 70.8 | 78.0 | 2.133002 | 12 | ||
| B | female | 76.022222 | 72.6 | 81.4 | 3.175601 | 9 | |
| male | 73.550000 | 66.6 | 81.8 | 4.818541 | 10 | ||
| C | female | 75.650000 | 75.6 | 75.7 | 0.070711 | 2 | |
| male | 71.700000 | 69.7 | 73.3 | 1.489966 | 4 | ||
| D | female | 75.025000 | 71.4 | 77.2 | 2.205027 | 8 | |
| male | 76.070000 | 69.5 | 85.7 | 5.153219 | 10 |
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.040726134954403075Example: 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()
| id | time | test1_score | test2_score | test3_score | |
|---|---|---|---|---|---|
| id | 1.000000 | 0.040144 | 0.738910 | 0.072236 | 0.024916 |
| time | 0.040144 | 1.000000 | 0.040726 | 0.061515 | -0.080249 |
| test1_score | 0.738910 | 0.040726 | 1.000000 | 0.004367 | 0.030067 |
| test2_score | 0.072236 | 0.061515 | 0.004367 | 1.000000 | 0.096539 |
| test3_score | 0.024916 | -0.080249 | 0.030067 | 0.096539 | 1.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.