數值替換、排序、刪除、插入
阿新 • • 發佈:2020-07-17
數值替換
1v1替換
df.replace('張天福','張三丰')
1對多替換
df.replace([269,365,456],211)
多對多
df.replace({1029:999,345:456})
區間替換
#apply把金額中>1000=>999,<500=>499
數值排序
指定行排序
#sort_values
df.sort_values(by='消費金額',ascending=True)#ascending升降序
df.sort_values(by=['消費金額','年齡'],ascending=[True,False])
行index
df.sort_index(ascending=False)
資料刪除
dropna
df.dropna()
列刪除
#drop
df.drop(['消費金額','年齡'],axis=1)
列索引刪除
df.drop(df.columns[[2,3]],axis=1)
df.drop(df.columns[1:4],axis=1)
行刪除
df.drop([0,2,3])
df.drop(df.index[1:4])
df.drop(['one','two','three'])
數值插入
行插入
#insert df.insert(2,'新列',[i for i in range(10)]) df['新的一列']=['新加列'for _ in range(10)]
分組列
bins=[0,1000,1500,3000]
lab=['1000以下','1000-1500','1500+']
df['使用者標籤']=pd.cut(df['消費金額'],bins,labels=lab)
apply
#apply用於Series
df['消費金額']=df['消費金額'].apply(lambda x:str(x)+'元')
applymap
df.applymap(lambda x:str(x)+'ok')