Python 数据分析与科学计算 第8章 Pandas数据分析处理.ppt
*********************************************8.4数据排序
原序列:591364178675578dtype:int64按索引升序排序:178364591578675dtype:int64按索引降序排序:675591578364178dtype:int64原数据帧:5227271917788189956575387688793按行索引名升序排序:5227189956575271917788387688793按列索引名降序排序:75222887191771758995653938768878.4数据排序
2.按值排序sort_values()函数sort_index(by,axis=0,level=None,ascending=True,inplace=False,kind=quicksort,na_position=last’)by:表示排序的列其他参数含义与按索引排序相同8.4数据排序
【例8.14】按值排序importpandasaspd?s=pd.Series([91,64,78,75,78],index=[5,3,1,6,5])#创建具有缺失值的序列print(原序列:\n,s)print(按值降序排序:\n,s.sort_values(ascending=False))data=pd.DataFrame([[71,91,77,88],[71,95,65,75],[87,68,87,93]],index=[1,2,3],columns=[高数,英语,物理,C语言])print(原数据帧:\n,data)#对行索引为1的数据进行升序排序print(按行值升序排序:\n,data.sort_values(by=1,axis=1))#对列索引“高数”为主索引,“英语”为次索引进行降序排序print(按列值降序排序:\n,data.sort_values(by=[高数,英语],ascending=False))8.4数据排序
原序列:59136417867578dtype:int64按值降序排序:591178578675364dtype:int64原数据帧:高数英语物理C语言171917788271956575387688793按行值升序排序:高数物理C语言英语171778891271657595387879368按列值降序排序:高数英语物理C语言3876887932719565751719177888.5缺失值处理
1.判断缺失值isnull()和notnull()函数【例8.15】判断缺失值importpandasaspdimportnumpyasnp?s=pd.Series([a,b,np.nan,c,None])#创建含有缺失值的序列print(创建的序列s:\n,s)print(s的缺失值:\n,s.isnull())#判断s的缺失值print(s中存在的缺失值和索引:\n,s[s.isnull()])#判断s中存在缺失值的列#创建含有缺失值的数据帧data=pd.DataFrame([[a,b,np.nan,c,5],[3,None,5,5,None],[1,2,3,4,5]])print(创建的数据帧data:\n,data)print(data的缺失值:\n,data.isnull())