본문으로 바로가기
반응형

파이썬으로 데이터프레임을 생성하여 출력할 때,

 

가끔씩 자리수가 원하지 않는 방식으로 출력되는 경우가 있다.

 

이때, pandas의 display.float_format 옵션을 설정해줌으로써 해결할 수 있다.

import pandas as pd

 

- Scientific notation 을 사용하지 않는 경우 (1234.56)

pd.options.display.float_format = '{:.2f}'.format
pd.set_option('display.float_format', '{:.2f}'.format)

- Scientific notation 을 사용하는 경우 (1.23e+03)

pd.set_option('display.float_format', '{:.2e}'.format)

- 상황에 맞게 출력되길 원하는 경우 (1234.56 or 1.23e+03)

pd.set_option('display.float_format', '${:.2g}'.format)

- 원래 상태로 되돌리길 원하는 경우

pd.set_option('display.float_format', None)
반응형