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 |
Tags
- MySQL
- 함수
- Unity
- 유니티
- ubuntu
- 리눅스
- 다이어트
- MS-SQL
- 라즈베리파이
- PER
- port
- swift
- Excel
- pandas
- IOS
- PyQt5
- node.js
- sqlite
- mssql
- urllib
- Linux
- python
- 날짜
- GIT
- javascript
- PyQt
- flutter
- tensorflow
- ASP
- 맛집
Archives
아미(아름다운미소)
메모리 사용량을 상세히 분석 본문
def analyze_memory_usage(df, warning_threshold=20):
"""
[최종 개선사항]
1. drop() 메서드 오류 해결
2. 인덱스/컬럼 처리 강화
3. 메모리 계산 최적화
"""
try:
# 1. 메모리 사용량 계산
mem_usage = df.memory_usage(deep=True)
total_memory = mem_usage.sum()
# 2. 분석 결과 생성
mem_analysis = pd.DataFrame({
'Memory (MB)': (mem_usage / (1024**2)).round(2),
'Percentage (%)': (mem_usage / total_memory * 100).round(2),
'Dtype': df.dtypes,
'Unique Values': df.nunique()
}, index=mem_usage.index)
# 3. 경고 컬럼 필터링
high_mem_cols = [
col for col in mem_analysis.index
if col in df.columns and
mem_analysis.loc[col, 'Percentage (%)'] > warning_threshold
]
if high_mem_cols:
print(f"⚠️ [경고] 다음 컬럼이 전체 메모리의 {warning_threshold}% 이상 사용:")
for col in high_mem_cols:
print(f" - {col}: {mem_analysis.loc[col, 'Percentage (%)']}% (타입: {df[col].dtype})")
# 4. 결과 출력 (drop() 대신 필요한 컬럼만 선택)
print("\n🔍 메모리 사용량 분석:")
display_cols = ['Memory (MB)', 'Percentage (%)', 'Dtype', 'Unique Values']
print(mem_analysis[display_cols])
# 5. 최적화 권장
print("\n💡 최적화 권장:")
for col in df.columns:
dtype = str(df[col].dtype)
nunique = df[col].nunique()
if dtype == 'object':
print(f" - '{col}': 범주형 변환 (고유값 {nunique}개)")
elif 'int' in dtype:
print(f" - '{col}': 정수형 다운캐스트 (현재: {dtype})")
elif 'float' in dtype:
print(f" - '{col}': 실수형 다운캐스트 (현재: {dtype})")
return mem_analysis
except Exception as e:
print(f"❌ 분석 실패: {str(e)}")
print(f"인덱스 샘플: {df.index[:5].tolist()}")
print(f"컬럼 목록: {df.columns.tolist()}")
return None
'랭귀지 > pandas' 카테고리의 다른 글
index.duplicated() 메서드 사용 (0) | 2025.03.31 |
---|---|
pandas_profiling (대규모 데이터 분석) (0) | 2025.03.28 |
df비교 (0) | 2025.03.27 |
데이터프레임의 메모리 사용량을 최적화하는 함수 (0) | 2025.03.27 |
메모리 최적화 자동화 (0) | 2025.03.27 |
Comments