pandas in action.png
本篇笔记为原书第八章节的内容。
- Reshaping and pivoting
本章开始对比论述了DataFrame数据格式(narrow OR wide)的优劣势,从而引出透视表的优势。
wide format.png
narrow format.png
透视表
-
读取数据
read data -
pivot_table() 对数据进行透视表处理 aggfunc参数(mean,sum,count,max,min,std,median,size)
pivot_table() -
对某列(支持多列)进行透视处理
对列进行透视处理 -
指定列标签值及空值填充值
colums && fill_value 各行各列进行统计总计处理 margins参数
margins 参数
-
支持多种统计方法同时进行
多种统计同时进行 -
对不同列进行不同的统计方法,aggfunc参数用字典对各列进行操作赋值
image.png -
index参数支持多值索引
multi index_col
索引Stackin和Unstacking操作
stack method moves an index level from the column axis to the row axis.
stack.png
unstack method moves the innermost level of the row index to the column index.
unstack
数据融合+透视处理
melt method. (Melting is the process of converting a wide data set to a narrow one.)
id_vars: 标识列
values_vars:哪些列需要melt操作并填写入新的一列
image.png
image.png
melt && pivot_table