시작하기무료로 시작하기

DataFrame에서 열 삭제하기

때로는 DataFrame에 더 이상 필요하지 않은 열이 있을 수 있어요. 불필요한 열을 제거해 DataFrame을 단순화해야 하는 경우가 자주 있습니다. 동영상에서 행이나 열을 삭제할 때 어떤 메서드를 사용해야 하는지 기억하시나요?

회사 이해관계자에게 PCE 계산 결과를 보여 주되, 계산에 사용한 열은 제외하려 한다고 가정해 봅시다. 제공된 DataFrame pce에서 PCDG, PCND, PCESV 열을 삭제해 PCE 열만 남기세요. 리스트 columns_to_drop가 제공됩니다.

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

금융을 위한 중급 Python

강의 보기

연습 안내

  • pce DataFrame의 열 이름을 출력하세요.
  • 리스트 columns_to_drop의 열을 제외하고 pce로부터 새로운 DataFrame을 만드세요.
  • 새 DataFrame의 열 이름을 출력하세요.
  • DataFrame pce에서 columns_to_drop의 열을 제자리에서(in place) 삭제하세요.

실습형 인터랙티브 연습

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

columns_to_drop = ['PCDG', 'PCND', 'PCESV']

# Print the current columns of the DataFrame pce
____(pce.columns)

# Create new_pce by dropping columns_to_drop from pce
new_pce = pce.drop(columns=____)
# Print the columns of the new DataFrame
print(new_pce.____)

# Drop the columns in_place in the original DataFrame
pce.____(columns=columns_to_drop, inplace=____)

# Print the columns of the DataFrame pce
print(pce.columns)
코드 편집 및 실행