반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- Apache
- Google Excel
- string
- array
- PANDAS
- numpy
- Excel
- google apps script
- Java
- Redshift
- list
- django
- gas
- matplotlib
- dataframe
- 파이썬
- hive
- Presto
- c#
- GIT
- PostgreSQL
- Github
- PySpark
- Tkinter
- Google Spreadsheet
- Kotlin
- Python
- math
- SQL
Archives
- Today
- Total
달나라 노트
Python Pandas : info(), memory_usage() (DataFrame의 memory 사용량 보기) 본문
Python/Python Pandas
Python Pandas : info(), memory_usage() (DataFrame의 memory 사용량 보기)
CosmosProject 2026. 5. 6. 23:29728x90
반응형
DataFrame의 memory 사용량을 보는 대표적인 방법 두가지를 알아보겠습니다.
1. info() method 이용
import pandas as pd
df_1 = pd.DataFrame({
'col1': [1, 1, 2, 2, 2, 3, 3, 3],
'col2': [4, 5, 6, 1, 8, 3, 5, 1],
'valid_yn': [1, 1, 1, 1, 1, 0, 0, 0],
})
# 주의사항: 데이터가 아주 클 경우, pandas는 성능을 위해 메모리 사용량을 추정치로만 보여줄 수 있습니다.
# 이때는 아래처럼 memory_usage='deep' 옵션을 추가해야 정확한 memory 사용량을 보여줍니다.
print(df_1.info(memory_usage='deep'))
-- Result
<class 'pandas.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 col1 8 non-null int64
1 col2 8 non-null int64
2 valid_yn 8 non-null int64
dtypes: int64(3)
memory usage: 324.0 bytes
None
info() method는 DataFrame의 다양한 정보를 보여주는데 그 중에 memory usage 정보가 포함되어 있습니다.
위 예시의 출력 결과를 보면 memory usage: 324.0 bytes라고 나오네요.
2. memory_usage() method 이용
import pandas as pd
df_1 = pd.DataFrame({
'col1': [1, 1, 2, 2, 2, 3, 3, 3],
'col2': [4, 5, 6, 1, 8, 3, 5, 1],
'valid_yn': [1, 1, 1, 1, 1, 0, 0, 0],
})
sr_memory_usage = df_1.memory_usage(deep=True)
print(type(sr_memory_usage))
print(sr_memory_usage)
-- Result
<class 'pandas.Series'>
Index 132
col1 64
col2 64
valid_yn 64
dtype: int64
memory 사용량 자체만을 보고싶을 땐 memory_usage() method를 사용하면 됩니다.
memory_usage() method는 DataFrame에 존재하는 컬럼 별 memory 사용량을 Series의 형태로 return합니다.
또한 단위는 기본적으로 Bytes입니다.
import pandas as pd
df_1 = pd.DataFrame({
'col1': [1, 1, 2, 2, 2, 3, 3, 3],
'col2': [4, 5, 6, 1, 8, 3, 5, 1],
'valid_yn': [1, 1, 1, 1, 1, 0, 0, 0],
})
sr_memory_usage = df_1.memory_usage(deep=True)
val_memory_usage = sum(sr_memory_usage)
print(str(val_memory_usage) + 'B')
-- Result
324B
컬럼별 memory 사용량을 보여주기 때문에 만약 DataFrame 전체가 얼마만큼의 용량을 가지고 있는지를 알려면 위처럼 sum()을 이용하여 Series에 담긴 메모리 사용량 값을 모두 더하면 됩니다.
728x90
반응형
'Python > Python Pandas' 카테고리의 다른 글
Comments
