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:
- Load and examine data with pandas
- Filter and sort data based on different conditions
- Create new columns from existing data
- Perform basic statistical analysis
- 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.
# Install required packages
!pip install kagglehub pandas numpy matplotlib seaborn --quiet
# 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.
# 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())
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.
# 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.")
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:
- Select 10 random rows from the dataset using the
sample()method - List all the unique conferences in the dataset
- Find out how many teams participated in each year
Include your code below:
# YOUR CODE HERE
# 1. Display 10 random samples
# 2. Find all unique conferences
# 3. Count teams by year
# 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())
Multiple Conditions¶
We can also filter based on multiple conditions using logical operators:
# 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())
Exercise 2: Filtering Practice¶
Your Task:
- Filter the dataset to find all teams from 2018 that made it to the Final Four (
POSTSEASON = 'F4') - Find all teams from the Big Ten conference (
CONF = 'B10') with a win rate of at least 70% - Identify the champions (
POSTSEASON = 'Champions') for each year in the dataset
Include your code below:
# 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.
# 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())
Exercise 3: Creating New Columns¶
Your Task:
- Create a new column called
Lthat shows the number of losses (Games - Wins) - Create a column
Win_Percentagethat shows the win ratio as a percentage (e.g., 0.75 should be 75%) - Create a column
Performancethat isExcellentif Win_Percentage is 80% or higher,Goodif 60-80%, andAverageif below 60%
Include your code below:
# 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.
# 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']])
Exercise 4: Sorting Practice¶
Your Task:
- Find the top 5 teams with the highest offensive efficiency (
ADJOE) in 2019 - Find the 5 teams with the lowest defensive efficiency (
ADJDE) in 2019 (remember, lower is better for defense!) - Sort teams by conference and then by win ratio within each conference
Include your code below:
# 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.
# 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())
Exercise 5: Aggregation Practice¶
Your Task:
- Find the average win ratio for each year in the dataset
- Calculate the average offensive and defensive efficiency by conference in 2019
- Find which year had the highest average 3-point shooting percentage (
3P_O)
Include your code below:
# 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
# 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()
Comparing Offensive and Defensive Efficiency¶
# 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()
NCAA Tournament Qualification¶
# 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()
Exercise 6: Data Visualization¶
Your Task:
- Create a bar chart showing the number of teams in each conference in 2019
- Create a scatter plot of Three-Point Shooting Percentage (
3P_O) vs. Win Ratio - 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:
# 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:
- Learning more advanced pandas functions like pivot tables, merging datasets
- Exploring other visualization libraries like Plotly
- Trying machine learning to predict outcomes based on data
- 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¶
- Pandas Documentation
- Python for Data Analysis by Wes McKinney
- Kaggle - Platform with datasets and tutorials
- DataCamp - Interactive courses on data science
Happy analyzing!