Pandas如何獲取數(shù)據(jù)的尺寸信息
Pandas獲取數(shù)據(jù)的尺寸信息
Pandas
中獲取數(shù)據(jù)的尺寸信息,比如我們有如下的Excel
數(shù)據(jù):
我們可以使用如下代碼來獲取數(shù)據(jù)的整體尺寸信息:
import pandas as pd file = pd.read_excel(r"C:\Users\15025\Desktop\uncle\debug.xlsx") print(file.size) print(file.shape) print(len(file)) """ result: 55 (11, 5) 11 """
可以看到,結(jié)果與numpy
包中的結(jié)果類似,當(dāng)我們的數(shù)據(jù)為二維時(shí),使用size
獲取到的是數(shù)據(jù)的整體大小,為行數(shù)量11
乘以列數(shù)量5
。
當(dāng)我們使用shape
時(shí),獲取到的是二維數(shù)據(jù)行數(shù)量與列數(shù)量組成的一個(gè)元組(11, 5)
。
當(dāng)我們使用len()
函數(shù)作用于二維數(shù)據(jù)時(shí),我們獲得的是行數(shù)量。
當(dāng)數(shù)據(jù)為一維時(shí),我們使用len()
函數(shù)獲取的結(jié)果將會(huì)與使用size
獲取到的結(jié)果一致。
pandas處理大數(shù)據(jù)信息
使用到的數(shù)據(jù)大小為130M
5 rows × 161 columns
g1.shape #(171907, 161) #17W的數(shù)據(jù),有161列
pandas 可以處理幾千萬(wàn),上億的數(shù)據(jù)
打印出每種類型占的內(nèi)存量
for dtype in ['float64','int64','object']: selected_dtype = g1.select_dtypes(include = [dtype]) mean_usage_b = selected_dtype.memory_usage(deep=True).mean() mean_usage_mb = mean_usage_b/1024**2 print('平均內(nèi)存占用 ',dtype , mean_usage_mb) ''' deep : bool,默認(rèn)為False 如果為True,則通過詢問對(duì)象 dtype 來深入了解數(shù)據(jù) 的系統(tǒng)級(jí)內(nèi)存消耗, 并將其包含在返回值中。 '''
讓內(nèi)存占用變小,int 類型從64 變?yōu)?32,在不影響使用的前提下
#查看每種類型最大 能表示多大的數(shù) int_types = ['uint8','int8','int16','int32','int64'] for it in int_types: print(np.iinfo(it))
g1_int = g1.select_dtypes(include = ['int64']) #生成一個(gè)只有int類型的DataFrame coverted_int = g1_int.apply(pd.to_numeric, downcast='unsigned') #apply 會(huì)將數(shù)據(jù)一條一條的讀取,并傳入目標(biāo)進(jìn)行執(zhí)行 #int64 轉(zhuǎn)換為了 unsigned
g1_float = g1.select_dtypes(include = ['float64']) #生成一個(gè)只有int類型的DataFrame coverted_floar = g1_int.apply(pd.to_numeric, downcast='float') #apply 會(huì)將數(shù)據(jù)一條一條的讀取,并傳入目標(biāo)進(jìn)行執(zhí)行 #float64轉(zhuǎn)換為了32
import pandas as pd g1 = pd.read_csv('game_logs.csv') g1_obj = g1.select_dtypes(include = ['object']) g1.shape #(171907, 78) g1_obj.describe() #查看信息生成的介紹 #count 數(shù)量 #unique 不重復(fù)的值 #top #freq
dow = g1_obj.day_of_week dow_cat = dow.astype('category') dow_cat.head()
優(yōu)化str占用內(nèi)存
converted_obj = pd.DataFrame() for col in g1_obj.columns: num_unique_values = len(g1_obj[col].unique()) num_total_values= len(g1_obj[col]) if num_unique_values / num_total_values < 0.5: converted_obj.loc[:,col] = g1_obj[col].astype('category') else: converted_obj.loc[:,col] = g1_obj[col]
#時(shí)間格式,寫成標(biāo)準(zhǔn)格式的是比較占用內(nèi)存的 #可以轉(zhuǎn)換時(shí)間格式 g1['date'] = pd.to_datetime(date,format='%Y%m%d') #這種比較占用內(nèi)存
結(jié)果:
def mem_usage(pandas_obj): if isinstance(pandas_obj,pd.DataFrame): usage_b = pandas_obj.memory_usage(deep=True).sum() else: usage_b = pandas_obj.memory_usagee(deep=True) usage_mb = usage_b/1024**2 return '{:03.2f} MB'.format(usage_mb) g1_int = g1.select_dtypes(include = ['int64']) #生成一個(gè)只有int類型的DataFrame coverted_int = g1_int.apply(pd.to_numeric, downcast='unsigned') #apply 會(huì)將數(shù)據(jù)一條一條的讀取,并傳入目標(biāo)進(jìn)行執(zhí)行 #int64 轉(zhuǎn)換為了 unsigned print(mem_usage(g1_int)) print(mem_usage(coverted_int))
7.87 MB
1.48 MB
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python pandas如何獲取數(shù)據(jù)的行數(shù)和列數(shù)
- Python?pandas數(shù)據(jù)預(yù)處理之行數(shù)據(jù)復(fù)制方式
- Python數(shù)據(jù)分析:pandas中Dataframe的groupby與索引用法
- Python pandas如何根據(jù)指定條件篩選數(shù)據(jù)
- pandas實(shí)現(xiàn)對(duì)一列/多列進(jìn)行數(shù)據(jù)區(qū)間篩選
- pandas數(shù)據(jù)合并與重塑之merge詳解
- Python-pandas返回重復(fù)數(shù)據(jù)的index問題
相關(guān)文章
Python區(qū)塊鏈創(chuàng)建Genesis Block教程
這篇文章主要為大家介紹了Python區(qū)塊鏈創(chuàng)建Genesis Block教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05基于OpenCV實(shí)現(xiàn)視頻循環(huán)播放
這篇文章主要為大家介紹了如何利用OpenCV實(shí)現(xiàn)視頻的循環(huán)播放,本文為大家提供了兩種方式,一個(gè)是利用Python語(yǔ)言實(shí)現(xiàn),一個(gè)是利用C++語(yǔ)言實(shí)現(xiàn),需要的可以參考一下2022-02-02基于python(urlparse)模板的使用方法總結(jié)
下面小編就為大家?guī)硪黄趐ython(urlparse)模板的使用方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Python數(shù)據(jù)結(jié)構(gòu)之Array用法實(shí)例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之Array用法實(shí)例,較為詳細(xì)的講述了Array的常見用法,具有很好的參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10Python數(shù)值求解微分方程方法(歐拉法,隱式歐拉)
這篇文章主要介紹了Python數(shù)值求解微分方程方法(歐拉法,隱式歐拉),文章圍繞主題展開詳細(xì)的內(nèi)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09跟老齊學(xué)Python之一個(gè)免費(fèi)的實(shí)驗(yàn)室
學(xué)習(xí)Python也要做實(shí)驗(yàn),也就是嘗試性地看看某個(gè)命令到底什么含義。在《集成開發(fā)環(huán)境(IDE)》一章中,我們介紹了Python的IDE時(shí),給大家推薦了IDLE,進(jìn)入到IDLE中,看到>>>符號(hào),可以在后面輸入一行指令。其實(shí),這就是一個(gè)非常好的實(shí)驗(yàn)室。2014-09-09python開發(fā)之thread線程基礎(chǔ)實(shí)例入門
這篇文章主要介紹了python開發(fā)之thread線程基礎(chǔ),以三個(gè)實(shí)例形式分析了Python中thread線程的基本使用方法,涉及串行與并行程序的執(zhí)行原理及線程的操作技巧,需要的朋友可以參考下2015-11-11