[python] 팬더 : 설정 번호 최대 행 수

다음을 보는 데 문제가 있습니다 DataFrame.

n = 100
foo = DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)
foo

문제는 ipython 노트북에서 기본값마다 모든 행을 인쇄하지는 않지만 결과 행을 보려면 슬라이스해야한다는 것입니다. 다음 옵션조차도 출력을 변경하지 않습니다.

pd.set_option('display.max_rows', 500)

누구든지 전체 배열을 표시하는 방법을 알고 있습니까?



답변

설정 display.max_rows:

pd.set_option('display.max_rows', 500)

이전 버전의 팬더 (<= 0.11.0)의 경우 display.height와를 모두 변경해야합니다 display.max_rows.

pd.set_option('display.height', 500)
pd.set_option('display.max_rows', 500)

도 참조하십시오 pd.describe_option('display').

다음과 같이이 옵션을 일시적으로 만 설정할 수 있습니다 .

from IPython.display import display
with pd.option_context('display.max_rows', 100, 'display.max_columns', 10):
    display(df) #need display to show the dataframe when using with in jupyter
    #some pandas stuff

다음과 같이 옵션을 기본값으로 다시 설정할 수도 있습니다.

pd.reset_option('display.max_rows')

그리고 모두 다시 재설정하십시오.

pd.reset_option('all')


답변

개인적으로, iPython 덕분에 탭 완성을 통해 쉽게 찾을 수 있으므로 할당 문으로 옵션을 직접 설정하는 것이 좋습니다. 정확한 옵션 이름이 무엇인지 기억하기가 어렵 기 때문에이 방법이 효과적입니다.

예를 들어 기억해야 할 것은 pd.options

pd.options.<TAB>

여기에 이미지 설명을 입력하십시오

대부분의 옵션은 display

pd.options.display.<TAB>

여기에 이미지 설명을 입력하십시오

여기에서 일반적으로 현재 값이 다음과 같이 출력됩니다.

pd.options.display.max_rows
60

그런 다음 원하는대로 설정했습니다.

pd.options.display.max_rows = 100

또한 옵션에 대한 컨텍스트 관리자를 알고 있어야합니다. 옵션은 코드 블록 내부에 옵션을 임시로 설정합니다. 옵션 이름을 문자열로 입력하고 원하는 값을 입력하십시오. 같은 줄에 여러 옵션을 전달할 수 있습니다.

with pd.option_context('display.max_rows', 100, 'display.max_columns', 10):
    some pandas stuff

다음과 같이 옵션을 기본값으로 다시 설정할 수도 있습니다.

pd.reset_option('display.max_rows')

그리고 모두 다시 재설정하십시오.

pd.reset_option('all')

를 통해 옵션을 설정하는 것이 여전히 좋습니다 pd.set_option. 난 그냥 직접 쉽게하고 덜 필요가있다 속성을 사용하여 발견 get_option하고 set_option.


답변

이미이 의견 과이 답변 에서 지적 되었지만 질문에 대한 직접적인 답변을 제공하려고 노력할 것입니다.

from IPython.display import display
import numpy as np
import pandas as pd

n = 100
foo = pd.DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)

with pd.option_context("display.max_rows", foo.shape[0]):
    display(foo)

pandas.option_context 는 pandas 0.13.1 ( pandas 0.13.1 릴리스 정보 ) 부터 사용할 수 있습니다 . 에 따르면 ,

with 블록을 종료하면 이전 설정으로 되돌아가는 일련의 옵션으로 코드 블록을 실행할 수 있습니다.


답변

@hanleyhansen이 주석에서 언급했듯이 0.18.1 버전에서는 display.height옵션이 더 이상 사용되지 않으며 ” display.max_rows대신 사용” 이라고 말합니다 . 따라서 다음과 같이 구성해야합니다.

pd.set_option('display.max_rows', 500)

참고 항목 릴리스 노트 – 0.18.1 문서 팬더 :

더 이상 사용되지 않는 display.height, display.width는 이제 형식화 옵션만으로 <0.11.0과 유사한 요약 트리거링을 제어하지 않습니다.


답변

pd.set_option('display.max_rows', 500)
df

Jupyter 에서는 작동하지 않습니다 !
대신 다음을 사용하십시오.

pd.set_option('display.max_rows', 500)
df.head(500)


답변

마찬가지로 이 답변 A와 비슷한 질문 , 설정을 해킹 할 필요가 없다. 작성하는 것이 훨씬 간단합니다.

print(foo.to_string())


답변