Pandas dataframe에서 인덱스 이름을 바꾸는 방법


다음 구문을 사용하여 Pandas DataFrame의 인덱스 열 이름을 바꿀 수 있습니다.

 df. index . rename (' new_index_name ', inplace= True )

다음 예에서는 실제로 이 구문을 사용하는 방법을 보여줍니다.

예: Pandas DataFrame에서 인덱스 이름 바꾸기

다음과 같은 팬더 DataFrame이 있다고 가정합니다.

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' points ': [25, 12, 15, 14, 19, 23, 25, 29],
                   ' assists ': [5, 7, 7, 9, 12, 9, 9, 4],
                   ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

   points assists rebounds
0 25 5 11
1 12 7 8
2 15 7 10
3 14 9 6
4 19 12 6
5 23 9 5
6 25 9 9
7 29 4 12

현재 DataFrame에는 인덱스 이름이 없습니다.

 #display index name
print ( df.index.name )

None

df.index.rename()을 사용하여 인덱스 이름을 바꿀 수 있습니다.

 #rename index
df. index . rename (' new_index ', inplace= True )

#view updated DataFrame
df

	   points assists rebounds
new_index			
0 25 5 11
1 12 7 8
2 15 7 10
3 14 9 6
4 19 12 6
5 23 9 5
6 25 9 9
7 29 4 12

inplace=True 는 pandas가 모든 원본 DataFrame 속성을 유지하도록 지시한다는 점에 유의하세요.

이제 DataFrame에 인덱스 이름이 있는지 확인할 수 있습니다.

 #display index name
print ( df.index.name )

new_index

추가 리소스

다음 튜토리얼에서는 Pandas에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.

Pandas DataFrame에 열을 추가하는 방법
Pandas DataFrame에서 첫 번째 열을 얻는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다