pandas
中 DataFrame
(即你用 pd.read_csv(data_path)
得到的 data
)常用方法的总结表格,包括你提到的 dropna()
和 sample()
,以及其他常用方法的功能和常见参数 🐍📊:
🧰 Pandas DataFrame 常用方法及参数表
方法名 | 功能描述 | 常见参数及说明 |
---|---|---|
dropna() | 删除缺失值所在的行或列 | axis=0 (行)或 1 (列);how='any' 或 'all' ;subset=['col1', 'col2'] |
sample() | 随机抽样 | n=5 (抽样数量);frac=0.1 (抽样比例);random_state=42 (随机种子) |
head() | 查看前几行数据 | n=5 (默认显示前5行) |
tail() | 查看最后几行数据 | n=5 (默认显示后5行) |
info() | 显示数据结构和缺失情况 | 无参数 |
describe() | 显示数值列的统计摘要 | include='all' (包括非数值列) |
value_counts() | 统计某列中每个值的频数(用于 Series) | normalize=True (返回比例);dropna=False (保留缺失值) |
groupby() | 分组操作 | by='col' (按某列分组);可链式使用 .agg() |
fillna() | 填充缺失值 | value=0 ;method='ffill' (前向填充);inplace=True |
rename() | 重命名列名或索引 | columns={'old':'new'} ;index={...} |
sort_values() | 按某列排序 | by='col' ;ascending=False |
drop() | 删除行或列 | labels=['col1'] ;axis=1 (列)或 0 (行);inplace=True |
apply() | 对每列或每行应用函数 | func=lambda x: ... ;axis=0 (列)或 1 (行) |
astype() | 类型转换 | {'col1': 'int', 'col2': 'float'} |
duplicated() | 检查重复行 | subset=['col1'] ;keep='first' |
reset_index() | 重置索引 | drop=True (不保留原索引);inplace=True |
这些方法可以组合使用,构建强大的数据清洗和分析流程。