시작하기무료로 시작하기

인덱스 설정 및 제거하기

pandas에서는 열을 인덱스로 지정할 수 있습니다. 이 기능을 사용하면 데이터 부분 추출 시, 코드가 더 간결해지며, 경우에 따라 데이터 조회 속도도 향상됩니다.

이번 챕터에서는 전 세계 도시의 평균 기온이 담긴 DataFrame, temperatures를 살펴볼 것입니다. pandaspd로 로드되어 있습니다.

이 연습은 강의의 일부입니다

pandas를 이용한 데이터 다루기

강의 보기

연습 안내

  • temperatures를 살펴보세요.
  • temperatures의 인덱스를 "city"로 설정하고, 결과를 temperatures_ind에 할당하세요.
  • temperatures_ind를 살펴보세요. temperatures와 어떻게 다른가요?
  • temperatures_ind의 인덱스를 초기화하되, 인덱스 값은 유지하세요.
  • temperatures_ind의 인덱스를 초기화하고, 인덱스 값도 삭제하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Look at temperatures
print(____)

# Set the index of temperatures to city
temperatures_ind = ____

# Look at temperatures_ind
print(____)

# Reset the temperatures_ind index, keeping its contents
print(____)

# Reset the temperatures_ind index, dropping its contents
print(____)
코드 편집 및 실행