Pandas初学者入门
一、思维导图(文字版)
Pandas(表格数据专用库)
├─ 1. 两种核心数据结构
│ ├─ Series → 一维带索引的数组(类似 Python 的 list + dict)
│ └─ DataFrame → 二维表格(多列 Series 的集合)
├─ 2. 创建 DataFrame / Series
│ ├─ 字典套列表 pd.DataFrame({'name':['A','B'],'age':[20,21]})
│ ├─ 列表套列表 pd.DataFrame([[1,2],[3,4]], columns=['a','b'])
│ └─ 从 NumPy 数组 pd.DataFrame(np.arange(9).reshape(3,3))
├─ 3. 常用属性(看一眼就懂)
│ ├─ df.shape # 行列尺寸
│ ├─ df.ndim # 维度(2)
│ ├─ df.size # 元素总数
│ ├─ df.index # 行索引
│ ├─ df.columns # 列索引
│ ├─ df.dtypes # 每列数据类型
│ ├─ df.head(n) # 前 n 行
│ └─ df.tail(n) # 后 n 行
├─ 4. 索引/列名 修改
│ ├─ df.index = ['A','B','C'] # 直接赋值
│ ├─ df.rename(index={'A':'AA'}, inplace=True) # 字典映射
│ └─ df.set_index('列名') # 把某列设为索引
├─ 5. 增删列
│ ├─ 新增列 df['city'] = ['北京','上海']
│ ├─ 指定位置插入 df.insert(2,'score',[80,90]) # 位置/列名/值
│ └─ 删除列 df.drop(columns=['A','B'])
├─ 6. 缺失值 NaN 处理
│ ├─ 检测:df.isnull() / df.notnull()
│ ├─ 删除:df.dropna(how='any'|'all') # 行含 NaN 就删
│ └─ 填充:df.fillna(0) / df.fillna(method='ffill') # 前向填充
├─ 7. 重复值处理
│ ├─ 检测:df.duplicated()
│ └─ 删除:df.drop_duplicates(subset=['A'])
├─ 8. 数据合并
│ ├─ concat(轴向堆叠)
│ │ ├─ 纵向 pd.concat([df1,df2]) # 行追加
│ │ └─ 横向 pd.concat([df1,df2], axis=1) # 列拼接
│ └─ merge / join(类 SQL 连接)
│ ├─ pd.merge(df1,df2, on='key') # 默认内连接
│ ├─ pd.merge(df1,df2, how='left') # 左连接
│ └─ df1.join(df2, how='outer') # 按索引连接
├─ 9. 取数(索引/切片)
│ ├─ 单列 df['name'] → Series
│ ├─ 多列 df[['name','age']] → DataFrame
│ ├─ 按行 df[0:3] # 行号切片
│ └─ 按标签 df.loc['row','col']
└─ 10. 与 NumPy 的桥梁├─ 创建:pd.DataFrame(np_array)└─ 运算:Series/DataFrame 可直接用 NumPy 函数
二、代码速查表(复制即可跑)
import pandas as pd
import numpy as np
from numpy import nan as NaN# 1. 创建 --------------------------------------------------------------
s = pd.Series([1, 3, 5, NaN], index=['a','b','c','d'])
df = pd.DataFrame({'name': ['James','Curry','Iverson'],'age': [18, 20, 19],'national': ['US','China','US']},index=['0','1','2'])# 2. 常用属性 ----------------------------------------------------------
print(df.shape) # (3,3)
print(df.dtypes) # 各列数据类型
print(df.head(2)) # 前两行
print(df.tail(1)) # 最后一行# 3. 修改索引/列名 -----------------------------------------------------
df.index = ['A','B','C']
df.rename(columns={'name':'player'}, inplace=True)# 4. 增删列 ------------------------------------------------------------
df['score'] = [80, 90, 85] # 直接加
df.insert(1, 'city', ['LA','BJ','SH']) # 指定位置插
df = df.drop(columns=['national']) # 删除列# 5. 缺失值处理 --------------------------------------------------------
df.loc['A','score'] = NaN
print(df.isnull()) # 检测
print(df.fillna(df.mean())) # 均值填充
print(df.dropna(how='any')) # 删除含 NaN 的行# 6. 重复值处理 --------------------------------------------------------
df_dup = pd.DataFrame({'A':[1,1,2], 'B':['x','x','y']})
print(df_dup.drop_duplicates())# 7. 数据合并 ----------------------------------------------------------
df1 = pd.DataFrame({'key':['a','b','c'], 'v1':[1,2,3]})
df2 = pd.DataFrame({'key':['a','b','d'], 'v2':[4,5,6]})
print(pd.merge(df1, df2, on='key', how='inner')) # 内连接
print(pd.concat([df1, df2], axis=0)) # 纵向堆叠# 8. 取数 --------------------------------------------------------------
print(df['age']) # 单列 → Series
print(df[['age','score']]) # 多列 → DataFrame
print(df.loc['A','city']) # 按标签取值
三、一句话总结
“Pandas 就是 Excel + SQL 的 Python 版:
用 DataFrame
当表,Series
当列,
增删改查、缺失值、重复值、合并连接,一行代码全搞定。”