Python中快速掌握Data Frame的常用操作
DataFrame基礎屬性有:values(元素)、index(索引)、columns(列名) 、dtypes(類型)、size(元素個數)、ndim(維度數)和 shape(形狀大小尺寸),還有使用T屬性 進行轉置
import pandas as pddetail=pd.read_excel(’E:datameal_order_detail.xlsx’) #讀取數據,使用read_excel 函數調用# print(detail)print('索引',detail.index)print('所以 值 :',detail.values)print('所以列名:',detail.columns)print('數據類型:',detail.dtypes)print('元素個數:',detail.size)print('維度:',detail.ndim)print('形狀大小 尺寸:',detail.shape)#使用T屬性 進行轉置print('轉置前的形狀:',detail.shape)數據print('轉置后的形狀:',detail.T.shape)二. 查改增刪DataFrame數據
查看訪問DataFramezhon’的數據(1.1)DataFrame數據的基本查看方式
#使用字典訪問方式order_id=detail[’order_id’]print('訂單詳情表的order_id的形狀:',order_id.shape)#使用訪問屬性的方式 dishes_name=detail.dishes_nameprint('訂單詳情表中的dishes_name的形狀:',dishes_name.shape)#DataFrame 單列多行的數據獲取dishes_name5=detail[’dishes_name’][:5]print(dishes_name5)#多列多行數據orderDish=detail[[’order_id’,’dishes_name’]][:5]print(orderDish)#訪問多行數據order5=detail[:][1:6]print('訂單詳情表中的1~6行元素的數據:n',order5)#使用DataFrame的head和tail方法獲取多行數據print(’訂單詳情表中前5行數據:n’,detail.head())#head()里面沒有參數的話,默認為5行print(’訂單詳情表中后5行數據:n’,detail.tail()) #tail()里面沒有參數的話,默認為5行
(1.2) .DataFrame的loc和iloc訪問方式;
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 實現多列切片orderDish1=detail.loc[:,[’order_id’,’dishes_name’]]print(orderDish1.size)orderDish2=detail.iloc[:,[1,3]]print(orderDish2.size)#使用loc、iloc 實現花式切片print('列名為order_id和dishes_name 的行名為3的數據:n',detail.loc[3,[’order_id’,’dishes_name’]])print(’列名為order_id和dishes_name 行名為2、3、4、5、6的數據為:n’,detail.loc[2:6,[’order_id’,’dishes_name’]])print(’列名1和3,行位置為3的數據為:n’,detail.iloc[3,[1,3]]) #這里為什么不可以loc函數, #因為loc函數傳入的是列索引的名稱(或行的名稱或條件),而iloc傳入的是位置print(’列位置為1和3,行位置為2,3,4,5,6的數據和:n’,detail.iloc[2:7,[1,3]])#這里是位置索引,7是取不到的#使用loc和iloc函數實現條件切片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列的數據為:n',detail.iloc[(detail[’order_id’]==458).values,[1,5]])#values 獲取元素 #使用iloc函數
(1.3).ix切片方法
#使用loc、iloc、ix 實現切片 比較(DataFrame.ix[行的索引或位置或條件,列索引名稱和位置])print(’列名為dishes_name行名為2,3,4,5,6的數據為:n’,detail.loc[2:6,[’dishes_name’]])print(’列位置為5行名為2~6的數據為:n’,detail.iloc[2:6,5])print(’列位置為5行名為2~6的數據為:n’,detail.ix[2:6,5])
2.更改DataFame中的數據
#將order_id為458 的改成 45800detail.loc[detail[’order_id’]==458,’order_id’] = 45800 #45800 這里 沒有單引號的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增添數據
#新增一列非定值detail[’payment’]=detail[’counts’]*detail[’amounts’]print(’detail新增列payment的前5行數據為:n’,detail[’payment’].head())#新增一列定值detail[’pay_way’]=’現金支付’print(’detail新增列的前5行的數據為:n’,detail[’pay_way’].head())``4.刪除某行或某列的數據(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的長度:’,len(detail))detail.drop(labels=range(1,11),axis=0,inplace=True)print(’刪除1~10行 后 detail的長度:’,len(detail))三. 描述分析DataFrame數據
1.數值特征的描述性統計describe()函數描述性統計2.類別類特征的描述性統計object類型,categroy類型
到此這篇關于Python中快速掌握Data Frame的常用操作的文章就介紹到這了,更多相關Python Data Frame的常用操作內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
