Pandas实战——数据透视及重构操作

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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容