College Basketball Data Analysis with Pandas: A Beginner's Tutorial

Introduction (15 minutes)

Welcome to this hands-on tutorial about data analysis with Python! In this 2.5-hour session, we'll learn how to use the pandas library to explore and analyze real-world data from NCAA college basketball.

This tutorial is designed for students who have basic Python knowledge but are new to data analysis. We'll work with a dataset containing statistics from college basketball teams between 2015-2019, and by the end of this session, you'll be able to:

  1. Load and examine data with pandas
  2. Filter and sort data based on different conditions
  3. Create new columns from existing data
  4. Perform basic statistical analysis
  5. Visualize relationships in the data

What is pandas?

Pandas is one of the most popular Python libraries for data analysis. It provides easy-to-use data structures and functions that make working with structured data intuitive. The two main data structures in pandas are:

  • Series: A one-dimensional labeled array (like a column in a spreadsheet)
  • DataFrame: A two-dimensional labeled data structure (like a spreadsheet)

Let's get started by setting up our environment and loading our data!

Part 1: Setting Up Our Environment (15 minutes)

First, we need to install and import the necessary libraries.

In [ ]:
# Install required packages
!pip install kagglehub pandas numpy matplotlib seaborn --quiet
In [ ]:
# Import the libraries we'll need
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
import os

Loading Our Dataset

Now let's load the college basketball dataset. In a real-world scenario, you might load data from a CSV file on your computer, but in this tutorial, we're using kagglehub to access the data directly.

In [ ]:
# Import libraries to load the dataset
import kagglehub
from kagglehub import KaggleDatasetAdapter

# Load the dataset
df = kagglehub.dataset_load(
    KaggleDatasetAdapter.PANDAS,
    "andrewsundberg/college-basketball-dataset",
    "cbb.csv",
)

# Display the first 5 rows to get a glimpse of the data
print("First 5 records:")
print(df.head())
Downloading from https://www.kaggle.com/api/v1/datasets/download/andrewsundberg/college-basketball-dataset?dataset_version_number=10&file_name=cbb.csv...
100%|██████████| 453k/453k [00:00<00:00, 2.27MB/s]
First 5 records:
             TEAM CONF   G   W  ADJOE  ADJDE  BARTHAG  EFG_O  EFG_D   TOR  \
0  North Carolina  ACC  40  33  123.3   94.9   0.9531   52.6   48.1  15.4   
1       Wisconsin  B10  40  36  129.1   93.6   0.9758   54.8   47.7  12.4   
2        Michigan  B10  40  33  114.4   90.4   0.9375   53.9   47.7  14.0   
3      Texas Tech  B12  38  31  115.2   85.2   0.9696   53.5   43.0  17.7   
4         Gonzaga  WCC  39  37  117.8   86.3   0.9728   56.6   41.1  16.2   

   ...  FTRD  2P_O  2P_D  3P_O  3P_D  ADJ_T   WAB  POSTSEASON  SEED  YEAR  
0  ...  30.4  53.9  44.6  32.7  36.2   71.7   8.6         2ND   1.0  2016  
1  ...  22.4  54.8  44.7  36.5  37.5   59.3  11.3         2ND   1.0  2015  
2  ...  30.0  54.7  46.8  35.2  33.2   65.9   6.9         2ND   3.0  2018  
3  ...  36.6  52.8  41.9  36.5  29.7   67.5   7.0         2ND   3.0  2019  
4  ...  26.9  56.3  40.0  38.2  29.0   71.5   7.7         2ND   1.0  2017  

[5 rows x 24 columns]

Part 2: Getting to Know Our Data (30 minutes)

Before diving into analysis, it's essential to understand what data we're working with. Let's examine the structure and contents of our dataset.

In [ ]:
# Get information about our DataFrame
print("\nDataFrame Information:")
df.info()

# Get a statistical summary of the numerical columns
print("\nSummary Statistics:")
print(df.describe())

# Check the shape (rows, columns) of our DataFrame
print("\nDataFrame Shape:")
print(f"The dataset has {df.shape[0]} rows and {df.shape[1]} columns.")
DataFrame Information:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3885 entries, 0 to 3884
Data columns (total 24 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   TEAM        3885 non-null   object 
 1   CONF        3885 non-null   object 
 2   G           3885 non-null   int64  
 3   W           3885 non-null   int64  
 4   ADJOE       3885 non-null   float64
 5   ADJDE       3885 non-null   float64
 6   BARTHAG     3885 non-null   float64
 7   EFG_O       3885 non-null   float64
 8   EFG_D       3885 non-null   float64
 9   TOR         3885 non-null   float64
 10  TORD        3885 non-null   float64
 11  ORB         3885 non-null   float64
 12  DRB         3885 non-null   float64
 13  FTR         3885 non-null   float64
 14  FTRD        3885 non-null   float64
 15  2P_O        3885 non-null   float64
 16  2P_D        3885 non-null   float64
 17  3P_O        3885 non-null   float64
 18  3P_D        3885 non-null   float64
 19  ADJ_T       3885 non-null   float64
 20  WAB         3885 non-null   float64
 21  POSTSEASON  748 non-null    object 
 22  SEED        748 non-null    float64
 23  YEAR        3885 non-null   int64  
dtypes: float64(18), int64(3), object(3)
memory usage: 728.6+ KB

Summary Statistics:
                 G            W        ADJOE        ADJDE      BARTHAG  \
count  3885.000000  3885.000000  3885.000000  3885.000000  3885.000000   
mean     30.531532    16.075418   103.339743   103.341519     0.493928   
std       3.809623     6.506570     7.315009     6.490684     0.255374   
min       5.000000     0.000000    76.600000    84.000000     0.005000   
25%      29.000000    11.000000    98.300000    98.600000     0.281200   
50%      31.000000    16.000000   103.000000   103.500000     0.474000   
75%      33.000000    21.000000   108.100000   108.000000     0.716400   
max      40.000000    38.000000   129.100000   124.000000     0.984200   

             EFG_O        EFG_D          TOR         TORD          ORB  ...  \
count  3885.000000  3885.000000  3885.000000  3885.000000  3885.000000  ...   
mean     49.935856    50.125946    18.551223    18.483115    29.267310  ...   
std       3.079803     2.855079     2.145057     2.289045     4.201366  ...   
min      39.200000    39.600000    11.900000    10.200000    14.400000  ...   
25%      47.900000    48.200000    17.100000    16.900000    26.500000  ...   
50%      49.900000    50.100000    18.500000    18.400000    29.300000  ...   
75%      52.000000    52.000000    19.900000    19.900000    32.000000  ...   
max      61.000000    60.100000    27.100000    28.500000    43.600000  ...   

               FTR         FTRD         2P_O         2P_D         3P_O  \
count  3885.000000  3885.000000  3885.000000  3885.000000  3885.000000   
mean     34.384813    34.645766    49.224402    49.388880    34.141133   
std       5.465945     6.337450     3.416380     3.290915     2.713812   
min      19.600000    16.200000    37.700000    37.700000    24.700000   
25%      30.500000    30.100000    46.900000    47.200000    32.300000   
50%      34.200000    34.000000    49.200000    49.400000    34.100000   
75%      38.000000    38.600000    51.400000    51.700000    35.900000   
max      58.600000    60.700000    64.000000    61.200000    44.100000   

              3P_D        ADJ_T          WAB        SEED         YEAR  
count  3885.000000  3885.000000  3885.000000  748.000000  3885.000000  
mean     34.319614    67.712227    -7.639967    8.798128  2018.403604  
std       2.406306     3.037251     6.826502    4.671615     3.583747  
min      26.100000    57.200000   -25.200000    1.000000  2013.000000  
25%      32.700000    65.700000   -12.600000    5.000000  2015.000000  
50%      34.300000    67.600000    -8.100000    9.000000  2018.000000  
75%      35.900000    69.700000    -3.000000   13.000000  2022.000000  
max      43.100000    83.400000    13.100000   16.000000  2024.000000  

[8 rows x 21 columns]

DataFrame Shape:
The dataset has 3885 rows and 24 columns.

Understanding the Features

Let's understand what each column in our dataset represents:

Feature Description
TEAM The Division I college basketball school
CONF The Athletic Conference in which the school participates
G Number of games played
W Number of games won
ADJOE Adjusted Offensive Efficiency (points scored per 100 possessions)
ADJDE Adjusted Defensive Efficiency (points allowed per 100 possessions)
BARTHAG Power Rating (Chance of beating an average Division I team)
EFG_O Effective Field Goal Percentage Shot
EFG_D Effective Field Goal Percentage Allowed
TOR Turnover Percentage Allowed (Turnover Rate)
TORD Turnover Percentage Committed (Steal Rate)
ORB Offensive Rebound Percentage
DRB Defensive Rebound Percentage
FTR Free Throw Rate (How often the given team shoots Free Throws)
FTRD Free Throw Rate Allowed
2P_O Two-Point Shooting Percentage
2P_D Two-Point Shooting Percentage Allowed
3P_O Three-Point Shooting Percentage
3P_D Three-Point Shooting Percentage Allowed
ADJ_T Adjusted Tempo (possessions per 40 minutes)
WAB Wins Above Bubble (margin for making the NCAA Tournament)
POSTSEASON Round where the team was eliminated
SEED Seed in the NCAA March Madness Tournament
YEAR Season

Exercise 1: Basic Data Exploration

Now, let's practice accessing and exploring the data on your own.

Your Task:

  1. Select 10 random rows from the dataset using the sample() method
  2. List all the unique conferences in the dataset
  3. Find out how many teams participated in each year

Include your code below:

In [ ]:
# YOUR CODE HERE
# 1. Display 10 random samples

# 2. Find all unique conferences

# 3. Count teams by year

Part 3: Filtering Data (30 minutes)

One of the most common operations in data analysis is filtering data based on certain conditions. In pandas, we can use boolean indexing to filter rows.

Filtering for a Specific Year

Let's filter our dataset to focus on the 2019 season:

In [ ]:
# Filter dataframe for 2019
df_19 = df[df['YEAR'] == 2019]

# Check how many records we have for 2019
print(f"Number of teams in 2019: {len(df_19)}")

# Display first few rows
print(df_19.head())
Number of teams in 2019: 353
          TEAM CONF   G   W  ADJOE  ADJDE  BARTHAG  EFG_O  EFG_D   TOR  ...  \
3   Texas Tech  B12  38  31  115.2   85.2   0.9696   53.5   43.0  17.7  ...   
8     Virginia  ACC  38  35  123.0   89.9   0.9736   55.2   44.7  14.7  ...   
20        Duke  ACC  38  32  118.9   89.2   0.9646   53.6   45.0  17.5  ...   
21      Purdue  B10  36  26  122.8   94.3   0.9539   53.6   49.0  15.8  ...   
31    Kentucky  SEC  37  30  117.5   89.8   0.9568   53.0   46.6  18.6  ...   

    FTRD  2P_O  2P_D  3P_O  3P_D  ADJ_T   WAB  POSTSEASON  SEED  YEAR  
3   36.6  52.8  41.9  36.5  29.7   67.5   7.0         2ND   3.0  2019  
8   26.3  52.5  45.7  39.5  28.9   60.7  11.1   Champions   1.0  2019  
20  24.0  58.0  45.0  30.8  29.9   73.6  11.2          E8   1.0  2019  
21  31.7  51.5  47.2  37.4  34.2   67.0   6.1          E8   3.0  2019  
31  26.8  52.9  43.6  35.4  34.3   66.9   8.8          E8   2.0  2019  

[5 rows x 24 columns]

Multiple Conditions

We can also filter based on multiple conditions using logical operators:

In [ ]:
# Teams that won more than 25 games in 2019
many_wins = df_19[df_19['W'] > 25]
print(f"Number of teams with more than 25 wins: {len(many_wins)}")
print(many_wins[['TEAM', 'W', 'G']].head())

# Teams from the ACC conference in 2019
acc_teams = df_19[df_19['CONF'] == 'ACC']
print(f"\nNumber of ACC teams in 2019: {len(acc_teams)}")
print(acc_teams[['TEAM', 'W', 'G']].head())
Number of teams with more than 25 wins: 36
          TEAM   W   G
3   Texas Tech  31  38
8     Virginia  35  38
20        Duke  32  38
21      Purdue  26  36
31    Kentucky  30  37

Number of ACC teams in 2019: 15
               TEAM   W   G
8          Virginia  35  38
20             Duke  32  38
141  Boston College  14  31
142         Clemson  20  34
143    Georgia Tech  14  32

Exercise 2: Filtering Practice

Your Task:

  1. Filter the dataset to find all teams from 2018 that made it to the Final Four (POSTSEASON = 'F4')
  2. Find all teams from the Big Ten conference (CONF = 'B10') with a win rate of at least 70%
  3. Identify the champions (POSTSEASON = 'Champions') for each year in the dataset

Include your code below:

In [ ]:
# YOUR CODE HERE
# 1. 2018 Final Four teams

# 2. Big Ten teams with at least 70% win rate

# 3. Champions for each year

Part 4: Creating New Columns (20 minutes)

Sometimes we need to create new columns based on existing data. For example, let's calculate the win ratio for each team.

In [ ]:
# Add a win ratio column
df['W_ratio'] = df['W'] / df['G']

# Let's see our new column
print(df[['TEAM', 'CONF', 'W', 'G', 'W_ratio']].head())

# We can round to make it more readable
df['W_ratio'] = df['W_ratio'].round(3)
print(df[['TEAM', 'CONF', 'W', 'G', 'W_ratio']].head())
             TEAM CONF   W   G   W_ratio
0  North Carolina  ACC  33  40  0.825000
1       Wisconsin  B10  36  40  0.900000
2        Michigan  B10  33  40  0.825000
3      Texas Tech  B12  31  38  0.815789
4         Gonzaga  WCC  37  39  0.948718
             TEAM CONF   W   G  W_ratio
0  North Carolina  ACC  33  40    0.825
1       Wisconsin  B10  36  40    0.900
2        Michigan  B10  33  40    0.825
3      Texas Tech  B12  31  38    0.816
4         Gonzaga  WCC  37  39    0.949

Exercise 3: Creating New Columns

Your Task:

  1. Create a new column called L that shows the number of losses (Games - Wins)
  2. Create a column Win_Percentage that shows the win ratio as a percentage (e.g., 0.75 should be 75%)
  3. Create a column Performance that is Excellent if Win_Percentage is 80% or higher, Good if 60-80%, and Average if below 60%

Include your code below:

In [ ]:
# YOUR CODE HERE
# 1. Number of losses

# 2. Win percentage

# 3. Performance categorization

Part 5: Sorting and Ranking (20 minutes)

Sorting is essential for finding the top or bottom performers in our dataset.

In [ ]:
# Sort teams by win ratio in descending order
top_teams = df.sort_values(by='W_ratio', ascending=False)
print("Top 10 teams by win ratio across all years:")
print(top_teams[['TEAM', 'YEAR', 'W', 'G', 'W_ratio']].head(10))

# We can sort by multiple columns
# For example, year (ascending) and then win ratio (descending)
yearly_top = df.sort_values(by=['YEAR', 'W_ratio'], ascending=[True, False])
print("\nTop team by win ratio for each year:")
print(yearly_top.drop_duplicates(subset=['YEAR'])[['TEAM', 'YEAR', 'W', 'G', 'W_ratio']])
Top 10 teams by win ratio across all years:
                   TEAM  YEAR   W   G  W_ratio
2795      Southern Utah  2021  20  19    1.053
3571        McNeese St.  2024  30  29    1.034
2828         Murray St.  2022  31  31    1.000
2458            Gonzaga  2021  26  26    1.000
2511  Abilene Christian  2021  23  23    1.000
49             Kentucky  2015  38  39    0.974
2115        Wichita St.  2014  34  35    0.971
3208       Oral Roberts  2023  30  31    0.968
2504           Winthrop  2021  23  24    0.958
2508            Liberty  2021  23  24    0.958

Top team by win ratio for each year:
               TEAM  YEAR   W   G  W_ratio
2131        Gonzaga  2013  31  34    0.912
2115    Wichita St.  2014  34  35    0.971
49         Kentucky  2015  38  39    0.974
10        Villanova  2016  35  40    0.875
4           Gonzaga  2017  37  39    0.949
2161       Virginia  2018  31  34    0.912
2104        Wofford  2019  30  32    0.938
2795  Southern Utah  2021  20  19    1.053
2828     Murray St.  2022  31  31    1.000
3208   Oral Roberts  2023  30  31    0.968
3571    McNeese St.  2024  30  29    1.034

Exercise 4: Sorting Practice

Your Task:

  1. Find the top 5 teams with the highest offensive efficiency (ADJOE) in 2019
  2. Find the 5 teams with the lowest defensive efficiency (ADJDE) in 2019 (remember, lower is better for defense!)
  3. Sort teams by conference and then by win ratio within each conference

Include your code below:

In [ ]:
# YOUR CODE HERE
# 1. Top offensive teams in 2019

# 2. Top defensive teams in 2019

# 3. Teams by conference and win ratio

Part 6: Aggregating Data (20 minutes)

Aggregation allows us to summarize our data across different groups.

In [ ]:
# Group by conference and calculate the average win ratio
conf_avg = df.groupby('CONF')['W_ratio'].mean().sort_values(ascending=False)
print("Average win ratio by conference:")
print(conf_avg.head(10))

# We can calculate multiple statistics at once
conf_stats = df.groupby('CONF').agg({
    'W_ratio': ['mean', 'min', 'max', 'count'],
    'ADJOE': 'mean',
    'ADJDE': 'mean'
})
print("\nDetailed conference statistics:")
print(conf_stats.head())
Average win ratio by conference:
CONF
B12     0.610544
B10     0.590367
BE      0.586782
Ind     0.586000
ACC     0.585932
SEC     0.575643
P12     0.565636
Amer    0.558430
MWC     0.558361
WCC     0.549648
Name: W_ratio, dtype: float64

Detailed conference statistics:
       W_ratio                           ADJOE       ADJDE
          mean    min    max count        mean        mean
CONF                                                      
A10   0.540522  0.143  0.818   157  105.400000  100.382166
ACC   0.585932  0.125  0.921   162  111.189506   97.320988
AE    0.479386  0.069  0.875   101   98.630693  106.024752
ASun  0.497159  0.100  0.958   107  101.022430  107.941121
Amer  0.558430  0.129  0.923   114  106.568421   99.407018

Exercise 5: Aggregation Practice

Your Task:

  1. Find the average win ratio for each year in the dataset
  2. Calculate the average offensive and defensive efficiency by conference in 2019
  3. Find which year had the highest average 3-point shooting percentage (3P_O)

Include your code below:

In [ ]:
# YOUR CODE HERE
# 1. Average win ratio by year

# 2. Average offensive and defensive efficiency by conference in 2019

# 3. Year with highest average 3-point percentage

Part 7: Basic Data Visualization (30 minutes)

Visualizing data helps us understand patterns and relationships more intuitively.

Distribution of Win Ratios

In [ ]:
# Create a histogram of win ratios
plt.figure(figsize=(10, 6))
plt.hist(df['W_ratio'], bins=20, color='skyblue', edgecolor='black')
plt.title('Distribution of Win Ratios')
plt.xlabel('Win Ratio')
plt.ylabel('Number of Teams')
plt.grid(True, alpha=0.3)
plt.show()
No description has been provided for this image

Comparing Offensive and Defensive Efficiency

In [ ]:
# Create a scatter plot of offensive vs defensive efficiency
plt.figure(figsize=(10, 6))
plt.scatter(df['ADJOE'], df['ADJDE'], alpha=0.5)
plt.title('Offensive vs Defensive Efficiency')
plt.xlabel('Adjusted Offensive Efficiency')
plt.ylabel('Adjusted Defensive Efficiency')
plt.grid(True, alpha=0.3)
# Add a note that lower defensive efficiency is better
plt.annotate('Lower is better for defense', xy=(110, 90), xytext=(110, 105),
             arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
No description has been provided for this image

NCAA Tournament Qualification

In [ ]:
# Create a new column to indicate if a team qualified for the NCAA tournament
df['NCAA_Qualifier'] = df['SEED'].notna()

# Create a scatter plot showing how offensive and defensive efficiency affects qualification
plt.figure(figsize=(10, 6))
sns.scatterplot(x='ADJOE', y='ADJDE', hue='NCAA_Qualifier', data=df, alpha=0.7)
plt.title('NCAA Tournament Qualification based on Efficiency')
plt.xlabel('Adjusted Offensive Efficiency')
plt.ylabel('Adjusted Defensive Efficiency')
plt.show()
No description has been provided for this image

Exercise 6: Data Visualization

Your Task:

  1. Create a bar chart showing the number of teams in each conference in 2019
  2. Create a scatter plot of Three-Point Shooting Percentage (3P_O) vs. Win Ratio
  3. Create a box plot comparing the win ratios of teams that qualified for the NCAA tournament vs. those that didn't

Include your code below:

In [ ]:
# YOUR CODE HERE
# Use AI help for this one, the code looks a bit complicated!
# 1. Bar chart of teams by conference

# 2. Scatter plot of three-point shooting vs win ratio

# 3. Box plot of win ratios by NCAA qualification

Conclusion (10 minutes)

Congratulations! You've now learned the fundamentals of data analysis with pandas:

  • Loading and examining data
  • Filtering and sorting
  • Creating new columns
  • Aggregating data
  • Basic visualization

These skills are incredibly valuable across many fields and will serve as a solid foundation as you continue your data analysis journey.

Next Steps

To further develop your skills, consider:

  1. Learning more advanced pandas functions like pivot tables, merging datasets
  2. Exploring other visualization libraries like Plotly
  3. Trying machine learning to predict outcomes based on data
  4. Working with your own datasets related to your interests

Remember, the best way to learn is through practice. Try applying these techniques to different datasets and real-world problems!

Additional Resources

Happy analyzing!