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ù)為二維時,使用size
獲取到的是數(shù)據(jù)的整體大小,為行數(shù)量11
乘以列數(shù)量5
。
當(dāng)我們使用shape
時,獲取到的是二維數(shù)據(jù)行數(shù)量與列數(shù)量組成的一個元組(11, 5)
。
當(dāng)我們使用len()
函數(shù)作用于二維數(shù)據(jù)時,我們獲得的是行數(shù)量。
當(dāng)數(shù)據(jù)為一維時,我們使用len()
函數(shù)獲取的結(jié)果將會與使用size
獲取到的結(jié)果一致。
pandas處理大數(shù)據(jù)信息
使用到的數(shù)據(jù)大小為130M
5 rows × 161 columns
g1.shape #(171907, 161) #17W的數(shù)據(jù),有161列
pandas 可以處理幾千萬,上億的數(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,默認為False 如果為True,則通過詢問對象 dtype 來深入了解數(shù)據(jù) 的系統(tǒng)級內(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']) #生成一個只有int類型的DataFrame coverted_int = g1_int.apply(pd.to_numeric, downcast='unsigned') #apply 會將數(shù)據(jù)一條一條的讀取,并傳入目標(biāo)進行執(zhí)行 #int64 轉(zhuǎn)換為了 unsigned
g1_float = g1.select_dtypes(include = ['float64']) #生成一個只有int類型的DataFrame coverted_floar = g1_int.apply(pd.to_numeric, downcast='float') #apply 會將數(shù)據(jù)一條一條的讀取,并傳入目標(biāo)進行執(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]
#時間格式,寫成標(biāo)準格式的是比較占用內(nèi)存的 #可以轉(zhuǎn)換時間格式 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']) #生成一個只有int類型的DataFrame coverted_int = g1_int.apply(pd.to_numeric, downcast='unsigned') #apply 會將數(shù)據(jù)一條一條的讀取,并傳入目標(biāo)進行執(zhí)行 #int64 轉(zhuǎn)換為了 unsigned print(mem_usage(g1_int)) print(mem_usage(coverted_int))
7.87 MB
1.48 MB
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python區(qū)塊鏈創(chuàng)建Genesis Block教程
這篇文章主要為大家介紹了Python區(qū)塊鏈創(chuàng)建Genesis Block教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05基于python(urlparse)模板的使用方法總結(jié)
下面小編就為大家?guī)硪黄趐ython(urlparse)模板的使用方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Python數(shù)據(jù)結(jié)構(gòu)之Array用法實例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之Array用法實例,較為詳細的講述了Array的常見用法,具有很好的參考借鑒價值,需要的朋友可以參考下2014-10-10Python數(shù)值求解微分方程方法(歐拉法,隱式歐拉)
這篇文章主要介紹了Python數(shù)值求解微分方程方法(歐拉法,隱式歐拉),文章圍繞主題展開詳細的內(nèi)介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09python開發(fā)之thread線程基礎(chǔ)實例入門
這篇文章主要介紹了python開發(fā)之thread線程基礎(chǔ),以三個實例形式分析了Python中thread線程的基本使用方法,涉及串行與并行程序的執(zhí)行原理及線程的操作技巧,需要的朋友可以參考下2015-11-11