pandas in action.png
本篇筆記為原書第八章節(jié)的內(nèi)容立莉。
- Reshaping and pivoting
本章開始對(duì)比論述了DataFrame數(shù)據(jù)格式(narrow OR wide)的優(yōu)劣勢(shì)嚣潜,從而引出透視表的優(yōu)勢(shì)。
wide format.png
narrow format.png
透視表
-
讀取數(shù)據(jù)
read data -
pivot_table() 對(duì)數(shù)據(jù)進(jìn)行透視表處理 aggfunc參數(shù)(mean,sum,count,max,min,std,median,size)
pivot_table() -
對(duì)某列(支持多列)進(jìn)行透視處理
對(duì)列進(jìn)行透視處理 -
指定列標(biāo)簽值及空值填充值
colums && fill_value 各行各列進(jìn)行統(tǒng)計(jì)總計(jì)處理 margins參數(shù)
margins 參數(shù)
-
支持多種統(tǒng)計(jì)方法同時(shí)進(jìn)行
多種統(tǒng)計(jì)同時(shí)進(jìn)行 -
對(duì)不同列進(jìn)行不同的統(tǒng)計(jì)方法聋呢,aggfunc參數(shù)用字典對(duì)各列進(jìn)行操作賦值
image.png -
index參數(shù)支持多值索引
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
數(shù)據(jù)融合+透視處理
melt method. (Melting is the process of converting a wide data set to a narrow one.)
id_vars: 標(biāo)識(shí)列
values_vars:哪些列需要melt操作并填寫入新的一列
image.png
image.png
melt && pivot_table