Python中快速掌握Data Frame的常用操作
DataFrame基礎(chǔ)屬性有:values(元素)、index(索引)、columns(列名) 、dtypes(類型)、size(元素個(gè)數(shù))、ndim(維度數(shù))和 shape(形狀大小尺寸),還有使用T屬性 進(jìn)行轉(zhuǎn)置
import pandas as pddetail=pd.read_excel(’E:datameal_order_detail.xlsx’) #讀取數(shù)據(jù),使用read_excel 函數(shù)調(diào)用# print(detail)print('索引',detail.index)print('所以 值 :',detail.values)print('所以列名:',detail.columns)print('數(shù)據(jù)類型:',detail.dtypes)print('元素個(gè)數(shù):',detail.size)print('維度:',detail.ndim)print('形狀大小 尺寸:',detail.shape)#使用T屬性 進(jìn)行轉(zhuǎn)置print('轉(zhuǎn)置前的形狀:',detail.shape)數(shù)據(jù)print('轉(zhuǎn)置后的形狀:',detail.T.shape)二. 查改增刪DataFrame數(shù)據(jù)
查看訪問(wèn)DataFramezhon’的數(shù)據(jù)(1.1)DataFrame數(shù)據(jù)的基本查看方式
#使用字典訪問(wèn)方式order_id=detail[’order_id’]print('訂單詳情表的order_id的形狀:',order_id.shape)#使用訪問(wèn)屬性的方式 dishes_name=detail.dishes_nameprint('訂單詳情表中的dishes_name的形狀:',dishes_name.shape)#DataFrame 單列多行的數(shù)據(jù)獲取dishes_name5=detail[’dishes_name’][:5]print(dishes_name5)#多列多行數(shù)據(jù)orderDish=detail[[’order_id’,’dishes_name’]][:5]print(orderDish)#訪問(wèn)多行數(shù)據(jù)order5=detail[:][1:6]print('訂單詳情表中的1~6行元素的數(shù)據(jù):n',order5)#使用DataFrame的head和tail方法獲取多行數(shù)據(jù)print(’訂單詳情表中前5行數(shù)據(jù):n’,detail.head())#head()里面沒有參數(shù)的話,默認(rèn)為5行print(’訂單詳情表中后5行數(shù)據(jù):n’,detail.tail()) #tail()里面沒有參數(shù)的話,默認(rèn)為5行
(1.2) .DataFrame的loc和iloc訪問(wèn)方式;
dishes_name1=detail.loc[:,’dishes_name’] #DataFrame.loc[行索引名稱或條件,列索引名稱]print('使用loc提取dishes_name列的size:',dishes_name1.size)dishes_name2=detail.iloc[:,3] #DataFrame.iloc[行索引位置,列索引位置]print('使用iloc提取第3列的size:',dishes_name2.size)#使用loc、iloc 實(shí)現(xiàn)多列切片orderDish1=detail.loc[:,[’order_id’,’dishes_name’]]print(orderDish1.size)orderDish2=detail.iloc[:,[1,3]]print(orderDish2.size)#使用loc、iloc 實(shí)現(xiàn)花式切片print('列名為order_id和dishes_name 的行名為3的數(shù)據(jù):n',detail.loc[3,[’order_id’,’dishes_name’]])print(’列名為order_id和dishes_name 行名為2、3、4、5、6的數(shù)據(jù)為:n’,detail.loc[2:6,[’order_id’,’dishes_name’]])print(’列名1和3,行位置為3的數(shù)據(jù)為:n’,detail.iloc[3,[1,3]]) #這里為什么不可以loc函數(shù), #因?yàn)閘oc函數(shù)傳入的是列索引的名稱(或行的名稱或條件),而iloc傳入的是位置print(’列位置為1和3,行位置為2,3,4,5,6的數(shù)據(jù)和:n’,detail.iloc[2:7,[1,3]])#這里是位置索引,7是取不到的#使用loc和iloc函數(shù)實(shí)現(xiàn)條件切片print(’detail中order_id為458的dishes_name為:n’,detail.loc[detail[’order_id’]==458,[’order_id’,’dishes_name’]]) #使用了locprint('detail中order_id為458 的第1、5列的數(shù)據(jù)為:n',detail.iloc[(detail[’order_id’]==458).values,[1,5]])#values 獲取元素 #使用iloc函數(shù)
(1.3).ix切片方法
#使用loc、iloc、ix 實(shí)現(xiàn)切片 比較(DataFrame.ix[行的索引或位置或條件,列索引名稱和位置])print(’列名為dishes_name行名為2,3,4,5,6的數(shù)據(jù)為:n’,detail.loc[2:6,[’dishes_name’]])print(’列位置為5行名為2~6的數(shù)據(jù)為:n’,detail.iloc[2:6,5])print(’列位置為5行名為2~6的數(shù)據(jù)為:n’,detail.ix[2:6,5])
2.更改DataFame中的數(shù)據(jù)
#將order_id為458 的改成 45800detail.loc[detail[’order_id’]==458,’order_id’] = 45800 #45800 這里 沒有單引號(hào)的print(’更改后detail中的order_id為 458 的:n’,detail.loc[detail[’order_id’]==458,’order_id’])print(’更改后detail中的order_id為 45800 的:n’,detail.loc[detail[’order_id’]==45800,’order_id’])detail.loc[detail[’order_id’]==45800,’order_id’] = 458
3.為DataFrame增添數(shù)據(jù)
#新增一列非定值detail[’payment’]=detail[’counts’]*detail[’amounts’]print(’detail新增列payment的前5行數(shù)據(jù)為:n’,detail[’payment’].head())#新增一列定值detail[’pay_way’]=’現(xiàn)金支付’print(’detail新增列的前5行的數(shù)據(jù)為:n’,detail[’pay_way’].head())``4.刪除某行或某列的數(shù)據(jù)(drop)#刪除某列print(’刪除pay_way前 detail中的列索引為:n’,detail.columns)detail.drop(labels=’pay_way’,axis=1,inplace=True)print(’刪除pay_way后 detail中的列索引為:n’,detail.columns)#刪除某幾行print(’刪除1~10行 前 detail的長(zhǎng)度:’,len(detail))detail.drop(labels=range(1,11),axis=0,inplace=True)print(’刪除1~10行 后 detail的長(zhǎng)度:’,len(detail))三. 描述分析DataFrame數(shù)據(jù)
1.數(shù)值特征的描述性統(tǒng)計(jì)describe()函數(shù)描述性統(tǒng)計(jì)2.類別類特征的描述性統(tǒng)計(jì)object類型,categroy類型
到此這篇關(guān)于Python中快速掌握Data Frame的常用操作的文章就介紹到這了,更多相關(guān)Python Data Frame的常用操作內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車3. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖4. .net如何優(yōu)雅的使用EFCore實(shí)例詳解5. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)6. python 爬取京東指定商品評(píng)論并進(jìn)行情感分析7. python基礎(chǔ)之匿名函數(shù)詳解8. Python獲取B站粉絲數(shù)的示例代碼9. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析10. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效
