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
- port
- Excel
- MS-SQL
- python
- PyQt
- 맛집
- PER
- IOS
- GIT
- ubuntu
- 리눅스
- javascript
- flutter
- 유니티
- urllib
- tensorflow
- Linux
- 함수
- 다이어트
- 날짜
- ASP
- node.js
- pandas
- PyQt5
- swift
- mssql
- sqlite
- Unity
- 라즈베리파이
Archives
아미(아름다운미소)
Type 변경 본문
cols = ['col1', 'col2', 'col3'] # category로 바꿀 컬럼 리스트
df[cols] = df[cols].apply(lambda x: x.astype('category'))
import pandas as pd
import numpy as np
# 예시 데이터 생성
df = pd.DataFrame({
'col1': np.random.choice(['apple', 'banana', 'cherry'], 1_000_000),
'col2': np.random.choice(['red', 'green', 'blue'], 1_000_000),
'col3': np.random.choice(['small', 'medium', 'large'], 1_000_000)
})
# 🔍 메모리 사용량 (변환 전)
print("변환 전 메모리 사용량:")
print(df.memory_usage(deep=True))
print("총합:", df.memory_usage(deep=True).sum() / 1024**2, "MB")
# 🔄 category 변환
df = df.astype({col: 'category' for col in ['col1', 'col2', 'col3']})
# 🔍 메모리 사용량 (변환 후)
print("\n변환 후 메모리 사용량:")
print(df.memory_usage(deep=True))
print("총합:", df.memory_usage(deep=True).sum() / 1024**2, "MB")
Comments