Pandas 基础
什么是 Pandas?
Section titled “什么是 Pandas?”Pandas 是基于 NumPy 构建的数据分析工具,提供了高效地操作大型数据集所需的工具。
pip install pandas导入 Pandas
Section titled “导入 Pandas”import pandas as pd核心数据结构
Section titled “核心数据结构”Series
Section titled “Series”一维数组,类似于带有标签的列表。
data = [1, 2, 3]s = pd.Series(data)print(s)DataFrame
Section titled “DataFrame”二维表格数据,类似于 Excel 表格。
data = { 'Name': ['Tom', 'Jerry', 'Mickey'], 'Age': [20, 22, 25]}df = pd.DataFrame(data)print(df)df.head() # 查看前 5 行df.info() # 查看数据基本信息df.describe() # 统计摘要读取与保存数据
Section titled “读取与保存数据”Pandas 支持多种数据格式,最常用的是 CSV。
读取 CSV
Section titled “读取 CSV”df = pd.read_csv('data.csv')保存 CSV
Section titled “保存 CSV”df.to_csv('output.csv', index=False)# 筛选 Age 大于 21 的行filtered_df = df[df['Age'] > 21]print(filtered_df)