當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Pandas Index.memory_usage()用法及代碼示例

Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。

Pandas Index.memory_usage()函數返回索引的內存使用情況。它返回索引中存在的所有單個標簽所使用的內存總和。

用法: Index.memory_usage(deep=False)

參數:
deep:深入反思數據,詢問對象dtype以獲取係統級內存消耗

返回:使用的字節

範例1:采用Index.memory_usage()函數查找Index對象使用的總內存。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Index 
idx = pd.Index(['Labrador', 'Beagle', 'Mastiff', 'Lhasa', 'Husky', 'Beagle']) 
  
# Print the Index 
idx

輸出:

現在我們將使用Index.memory_usage()函數查找idx對象的內存使用情況。

# finding the memory used by the idx object 
idx.memory_usage()

輸出:

該函數返回的值48表示正在使用48個字節的內存。

範例2:采用Index.memory_usage()函數檢查MultiIndex對象的內存使用情況。

# importing pandas as pd 
import pandas as pd 
  
# Creating the MutiIndex 
midx = pd.MultiIndex.from_arrays([['Mon', 'Tue', 'Wed', 'Thr'], [10, 20, 30, 40]], 
                                                       names =('Days', 'Target')) 
  
# Print the MultiIndex 
midx

輸出:

現在,我們將檢查midx對象使用的內存量。

# return the total memory used by the multi-index object 
midx.memory_usage()

輸出:

從輸出中可以看到,該函數返回了180,表示midx對象正在使用180個字節的內存。



相關用法


注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Index.memory_usage()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。