Python中快速掌握Data Frame的常用操作
掌握Data Frame的常用操作
一. 查看DataFrame的常用屬性
DataFrame基礎(chǔ)屬性有:values(元素)、index(索引)、columns(列名) 、dtypes(類型)、size(元素個數(shù))、ndim(維度數(shù))和 shape(形狀大小尺寸),還有使用T屬性 進(jìn)行轉(zhuǎn)置
import pandas as pd
detail=pd.read_excel('E:\data\meal_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("元素個數(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ù)
查看訪問DataFramezhon'的數(shù)據(jù)
(1.1)DataFrame數(shù)據(jù)的基本查看方式
#使用字典訪問方式
order_id=detail['order_id']
print("訂單詳情表的order_id的形狀:",order_id.shape)
#使用訪問屬性的方式
dishes_name=detail.dishes_name
print("訂單詳情表中的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)
#訪問多行數(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訪問方式;
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']]) #使用了loc
print("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 的改成 45800
detail.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增添數(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的長度:',len(detail))
detail.drop(labels=range(1,11),axis=0,inplace=True)
print('刪除1~10行 后 detail的長度:',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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Pandas實(shí)現(xiàn)MySQL窗口函數(shù)的解決方法
本文主要介紹 MySQL 中的窗口函數(shù)row_number()、lead()/lag()、rank()/dense_rank()、first_value()、count()、sum()如何使用pandas實(shí)現(xiàn),同時(shí)二者又有什么區(qū)別,感興趣的朋友一起看看吧2023-02-02
Sublime Text3最新激活注冊碼分享適用2020最新版 親測可用
這篇文章主要介紹了Sublime Text3最新激活注冊碼分享親測3211可用2020-11-11
python實(shí)現(xiàn)獲取客戶機(jī)上指定文件并傳輸?shù)椒?wù)器的方法
這篇文章主要介紹了python實(shí)現(xiàn)獲取客戶機(jī)上指定文件并傳輸?shù)椒?wù)器的方法,涉及Python實(shí)現(xiàn)C/S架構(gòu)程序與socket程序的使用技巧,需要的朋友可以參考下2015-03-03
利用Python實(shí)現(xiàn)多種風(fēng)格的照片處理
這篇文章主要為大家詳細(xì)介紹了如何利用Python一鍵實(shí)現(xiàn)多種風(fēng)格的照片處理并制作可視化GUI界面,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-07-07
Keras SGD 隨機(jī)梯度下降優(yōu)化器參數(shù)設(shè)置方式
這篇文章主要介紹了Keras SGD 隨機(jī)梯度下降優(yōu)化器參數(shù)設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python中Numpy和Matplotlib的基本使用指南
numpy庫處理的最基礎(chǔ)數(shù)據(jù)類型是由同種元素構(gòu)成的多維數(shù)組(ndarray),而matplotlib 是提供數(shù)據(jù)繪圖功能的第三方庫,其pyplot子庫主要用于實(shí)現(xiàn)各種數(shù)據(jù)展示圖形的繪制,這篇文章主要給大家介紹了關(guān)于Python中Numpy和Matplotlib的基本使用指南,需要的朋友可以參考下2021-11-11

