Python/일반

[Python] 데이터프레임 결과, 아웃풋 생략 없이 출력하기 pandas.set_option

공부하는 sum 2022. 7. 21. 09:19
728x90

데이터를 출력하다보면 밑에와 아웃풋이 처음 몇 개, 마지막 몇 개만 나오는 경우가 굉장히 흔하게 발생한다.

하지만 나온 출력을 모두 확인하고 싶을 때가 있는 법이다. 

그럴 때 사용할 수 있는 것은 바로 pandas의 set_option 

https://pandas.pydata.org/pandas-docs/version/0.23/generated/pandas.set_option.html

 

pandas.set_option — pandas 0.23.1 documentation

Regexp which should match a single option. Note: partial matches are supported for convenience, but unless you use the full option name (e.g. x.y.z.option_name), your code may break in future versions if new options with similar names are introduced.

pandas.pydata.org

여러가지 옵션들이 있는데, 

내가 주로 사용하는 옵션은 display의 max_columns, max_rows, max_seq_itmes 이렇게 세가지이다. 

pd.set_option(옵션명,개수) 이렇게 사용하면 된다.

import pandas as pd

# 행 출력을 n개까지 보고 싶을 때 
# n : int, 기본 100
pd.set_option('display.max_rows',n)

# 열 출력을 n개까지 보고 싶을 때 
# n : int, 기본 100
pd.set_option('display.max_columns',n)

# 값을 n개까지 보고 싶을 때
# n : int or None
pd.set_option('display.max_seq_items',n)

위의 max_rows와 max_columns는 데이터프레임에서 행과 열을 모두 출력해서 볼 때 주로 사용하고, 
마지막의 max_seq_items는 df.columns와 같은 df형태로 나오지 않는 출력을 모두 보고자할 때 사용한다. 

참고로 max_rows와 max_columns의 기본 값은 100으로 설정 되어 있고, 
display.max_seq_items의 경우 n 값을 None으로 주면 최대치로 출력된다.

pd.set_option('display.max_seq_item',None) 을 적용한 결과, 맨 처음과 다르게 모두 출력된 것을 볼 수 있다. 너무 길어서 text editor로 가야 다 볼 수 있지만!

 

728x90