pandas 데이터프레임을 콘솔에 가독성 좋게 출력하는 방법
Post

pandas 데이터프레임을 콘솔에 가독성 좋게 출력하는 방법

pandas 데이터프레임을 콘솔에 출력 시 tabulate 를 이용해 가독성을 올릴 수 있다.

tabulate 설치

1
pip install tabulate

한글의 경우 영어와 달리 줄간격이 달라 깨지므로 추가로 tabulate[widechars]를 추가로 설치해준다.

1
pip install 'tabulate[widechars]'

사용 예시

1
2
3
4
5
6
7
8
9
10
11
12
import pandas as pd
from tabulate import tabulate

tabulate.WIDE_CHARS_MODE = False

dict_test = {
    'col1': [1, 2, 3, 4, 5],
    'col2': ['삼성전자', '유한양행', '두산', '우리은행', '하이트진로홀딩스'],
    'col3': ['50000', '100000', '120000', '9900', '1398'],
}
df_test = pd.DataFrame(dict_test)
print(tabulate(df_test, headers='keys', tablefmt='github', showindex=True))

출력 결과

아래와 같이 깔끔하게 출력된다.

1
2
3
4
5
6
7
8
9
+----+--------+------------------+--------+
|    |   col1 | col2             |   col3 |
|----+--------+------------------+--------|
|  0 |      1 | 삼성전자           |  50000 |
|  1 |      2 | 유한양행           | 100000 |
|  2 |      3 | 두산              | 120000 |
|  3 |      4 | 우리은행           |   9900 |
|  4 |      5 | 하이트진로홀딩스     |   1398 |
+----+--------+------------------+--------+