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 可以處理幾千萬,上億的數(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編寫的五個(gè)拿來就能用的炫酷表白代碼分享
七夕快到了,所以本文小編將給給大家介紹五種拿來就能用的炫酷表白代碼,無限彈窗表白,愛心發(fā)射,心動(dòng)表白,玫瑰花等表白代碼,需要的小伙伴快來試試吧2023-08-08
在?Python?中使用變量創(chuàng)建文件名的方法
這篇文章主要介紹了在?Python?中使用變量創(chuàng)建文件名,格式化的字符串文字使我們能夠通過在字符串前面加上 f 來在字符串中包含表達(dá)式和變量,本文給大家詳細(xì)講解,需要的朋友可以參考下2023-03-03
Python 時(shí)間處理datetime實(shí)例
Python Cook書中有很多章節(jié)都是針對(duì)某個(gè)庫的使用進(jìn)行介紹或是通過組合多個(gè)函數(shù)實(shí)現(xiàn)一些復(fù)雜的功能。我這里直接跳過了上一章節(jié)中對(duì)于文件處理的一些章節(jié),直接進(jìn)入對(duì)時(shí)間操作的章節(jié)。2008-09-09
Python實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)采集新型冠狀病毒數(shù)據(jù)實(shí)例
在本篇文章里小編給大家整理了關(guān)于Python實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)采集新型冠狀病毒數(shù)據(jù)實(shí)例內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2020-02-02

