使用Pandas探索NBA数据集:从入门到实践
2026-02-04 04:40:35作者:管翌锬
环境准备
在开始之前,我们需要确保环境已经正确配置。Pandas是Python数据分析的核心库,通常与NumPy和Matplotlib一起使用。
安装依赖
可以通过pip或conda安装所需依赖:
pip install pandas numpy matplotlib
或者使用conda:
conda install pandas numpy matplotlib
初识Pandas
让我们从一个简单的NBA数据集开始探索Pandas的强大功能。
加载数据
首先下载并加载NBA历史比赛数据:
import pandas as pd
# 读取CSV文件
nba = pd.read_csv("nba_all_elo.csv")
数据概览
了解数据的基本信息:
# 查看数据类型
print(type(nba)) # <class 'pandas.core.frame.DataFrame'>
# 数据集大小
print(len(nba)) # 126314条记录
print(nba.shape) # (126314, 23) 表示126314行23列
# 查看前几行数据
print(nba.head())
# 查看后几行数据
print(nba.tail())
数据展示设置
调整Pandas的显示选项以获得更好的查看体验:
# 显示所有列
pd.set_option("display.max.columns", None)
# 设置显示精度
pd.set_option("display.precision", 2)
深入了解数据
数据类型分析
使用.info()方法查看各列的数据类型和缺失值情况:
nba.info()
基本统计信息
.describe()方法提供数值列的统计摘要:
nba.describe()
对于非数值列:
import numpy as np
nba.describe(include=object)
数据探索实践
让我们进行一些实际的数据探索:
# 各球队比赛场次统计
print(nba["team_id"].value_counts())
# 各球队名称出现次数
print(nba["fran_id"].value_counts())
# 湖人队的不同球队ID统计
print(nba.loc[nba["fran_id"] == "Lakers", "team_id"].value_counts())
日期处理
将比赛日期转换为datetime类型并进行时间分析:
nba["date_played"] = pd.to_datetime(nba["date_game"])
# 明尼阿波利斯湖人队(MNL)的比赛时间范围
print(nba.loc[nba["team_id"] == "MNL", "date_played"].agg(("min", "max")))
Pandas数据结构详解
Series:Pandas的基础构建块
Series是Pandas中的一维数据结构:
revenues = pd.Series([5555, 7000, 1980])
print(revenues.values) # 查看值数组
print(revenues.index) # 查看索引
可以自定义索引:
city_revenues = pd.Series(
[4200, 8000, 6500], index=["Amsterdam", "Toronto", "Tokyo"]
)
Series与Python数据结构的比较
Series与字典类似但功能更强大:
city_employee_count = pd.Series({"Amsterdam": 5, "Tokyo": 8})
print("Tokyo" in city_employee_count) # True
print("New York" in city_employee_count) # False
DataFrame:Pandas的核心数据结构
DataFrame是二维表格型数据结构:
city_data = pd.DataFrame(
{"revenue": city_revenues, "employee_count": city_employee_count}
)
查看DataFrame的基本属性:
print(city_data.index) # 行索引
print(city_data.columns) # 列名
print(city_data.values) # 值数组
数据访问技巧
Series数据访问
有多种方式访问Series中的数据:
# 通过索引标签
print(city_revenues["Toronto"]) # 8000
# 通过位置
print(city_revenues[1]) # 8000
print(city_revenues[-1]) # 6500
# 切片操作
print(city_revenues[1:])
print(city_revenues["Toronto":])
使用.loc和.iloc
.loc基于标签,.iloc基于位置:
colors = pd.Series(
["red", "purple", "blue", "green", "yellow"], index=[1, 2, 3, 5, 8]
)
print(colors.loc[1]) # 'red' (标签为1的值)
print(colors.iloc[1]) # 'purple' (第二个位置的值)
print(colors.iloc[1:3]) # 位置1到2
print(colors.loc[3:8]) # 标签3到8
DataFrame数据访问
访问列数据:
print(city_data["revenue"])
print(city_data.revenue) # 等效的简写形式
访问行数据:
print(city_data.loc["Amsterdam"]) # 通过标签
print(city_data.iloc[1]) # 通过位置
同时选择行和列:
print(city_data.loc["Amsterdam":"Tokyo", "revenue"])
实践练习
- 显示NBA数据集的最后3行
- 计算波士顿凯尔特人队在所有比赛中的总得分
- 显示NBA数据集的倒数第二行
通过这些基础操作,您已经掌握了使用Pandas进行数据探索的基本技能。Pandas的强大功能远不止于此,后续可以深入学习数据清洗、转换、分组聚合等高级操作。
登录后查看全文
热门项目推荐
相关项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0132- DDeepSeek-V4-ProDeepSeek-V4-Pro(总参数 1.6 万亿,激活 49B)面向复杂推理和高级编程任务,在代码竞赛、数学推理、Agent 工作流等场景表现优异,性能接近国际前沿闭源模型。Python00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
MiniCPM-V-4.6这是 MiniCPM-V 系列有史以来效率与性能平衡最佳的模型。它以仅 1.3B 的参数规模,实现了性能与效率的双重突破,在全球同尺寸模型中登顶,全面超越了阿里 Qwen3.5-0.8B 与谷歌 Gemma4-E2B-it。Jinja00
MiniMax-M2.7MiniMax-M2.7 是我们首个深度参与自身进化过程的模型。M2.7 具备构建复杂智能体应用框架的能力,能够借助智能体团队、复杂技能以及动态工具搜索,完成高度精细的生产力任务。Python00
AionUi免费、本地、开源的 24/7 全天候 Cowork 应用,以及适用于 Gemini CLI、Claude Code、Codex、OpenCode、Qwen Code、Goose CLI、Auggie 等的 OpenClaw | 🌟 喜欢就点star吧TypeScript05
项目优选
收起
暂无描述
Dockerfile
724
4.65 K
Ascend Extension for PyTorch
Python
596
749
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
425
376
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
991
980
暂无简介
Dart
968
246
Oohos_react_native
React Native鸿蒙化仓库
C++
345
391
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
912
132
deepin linux kernel
C
29
16
昇腾LLM分布式训练框架
Python
159
188
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.65 K
969