Python | Pandas Index.memory_usage ()

Опубликовано: 28 Марта, 2022

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

Pandas Index.memory_usage() function return the memory usage of the Index. It returns the sum of the memory used by all the individual labels present in the Index.

Syntax: Index.memory_usage(deep=False)

Parameters :
deep : Introspect the data deeply, interrogate object dtypes for system-level memory consumption

Returns : bytes used

Example #1: Use Index.memory_usage() function to find the overall memory used by the Index object.

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

Выход :

Now we will use Index.memory_usage() function to find the memory usage of the idx object.

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

Output :

The function has returned the value of 48 indicating that 48 bytes of memory are being used.
 
Example #2: Use Index.memory_usage() function to check the memory usage of the MultiIndex object.

# 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

Выход :

Now we will check the amount of memory used by the midx object.

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

Выход :

Как видно из выходных данных, функция вернула 180, указывая, что объект midx использует 180 байт памяти.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.