Pandas 옵션 및 설정

Beginner

This tutorial is from open-source community. Access the source code

소개

이 랩에서는 Pandas DataFrame 표시, 데이터 동작 등과 관련된 전역 동작을 구성하고 사용자 정의하는 방법을 이해하는 데 중점을 둡니다. 옵션을 가져오고/설정하고, 옵션을 기본값으로 재설정하고, 옵션을 설명하는 방법을 살펴봅니다. 또한 실행 후 이전 설정으로 되돌아가는 옵션 집합으로 코드 블록을 실행하는 방법도 배웁니다.

VM 팁

VM 시작이 완료되면 왼쪽 상단을 클릭하여 Notebook 탭으로 전환하여 실습을 위해 Jupyter Notebook에 액세스하십시오.

때로는 Jupyter Notebook 이 로딩을 완료하는 데 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한 사항으로 인해 작업의 유효성 검사는 자동화할 수 없습니다.

학습 중에 문제가 발생하면 언제든지 Labby 에게 문의하십시오. 세션 후 피드백을 제공해주시면 문제를 신속하게 해결해 드리겠습니다.

Pandas 가져오기

Pandas 라이브러리를 가져오는 것으로 시작해 보겠습니다. 이것은 Python 에서 강력한 데이터 조작 라이브러리입니다.

## Importing pandas library
import pandas as pd

옵션 가져오기 및 설정하기

pd.get_option 또는 pd.set_option을 사용하여 단일 옵션의 값을 가져오거나 설정할 수 있습니다. 여기서는 최대 표시 행을 999 로 설정합니다.

## Get the current setting for maximum display rows
print(pd.options.display.max_rows)

## Set the maximum display rows to 999
pd.options.display.max_rows = 999

## Verify the new setting
print(pd.options.display.max_rows)

옵션 재설정하기

하나 이상의 옵션을 기본값으로 재설정하려면 pd.reset_option을 사용할 수 있습니다.

## Reset the maximum display rows to default
pd.reset_option("display.max_rows")

## Verify the reset
print(pd.options.display.max_rows)

옵션 설명하기

하나 이상의 옵션에 대한 설명을 출력하려면 pd.describe_option을 사용하십시오.

## Describe the 'display.max_rows' option
pd.describe_option("display.max_rows")

option_context 사용하기

option_context 함수를 사용하면 실행 후 이전 설정으로 되돌아가는 옵션 집합으로 코드 블록을 실행할 수 있습니다.

## Execute a code block with a set of options
with pd.option_context("display.max_rows", 10):
    ## This will print 10 despite the global setting being different
    print(pd.get_option("display.max_rows"))

## This will print the global setting as the context block has ended
print(pd.get_option("display.max_rows"))

시작 옵션 설정하기

pandas 를 가져오고 옵션을 설정하는 시작 스크립트를 Python/IPython 환경에서 생성하여 pandas 작업을 더욱 효율적으로 만들 수 있습니다.

## This is an example of a startup script
## Place this in a .py file in the startup directory of IPython profile
import pandas as pd

pd.set_option("display.max_rows", 999)
pd.set_option("display.precision", 5)

요약

이 랩 가이드에서는 pandas 에서 옵션을 가져오고, 설정하고, 재설정하는 방법을 설명했습니다. 또한 옵션을 설명하고 option_context 함수를 사용하는 방법에 대해서도 논의했습니다. 마지막으로, Python/IPython 환경에서 시작 옵션을 설정하는 방법을 살펴보았습니다. 이러한 기술을 통해 필요에 맞게 pandas 의 동작을 사용자 정의하고 구성할 수 있습니다.