logo

Pandas Dataframe에서 인덱스 재설정

Pandas DataFrame에서 인덱스를 재설정하는 방법을 살펴보겠습니다. 종종 우리는 거대한 데이터 프레임으로 시작합니다. 팬더 데이터 프레임을 조작/필터링한 후에는 훨씬 더 작은 데이터 프레임이 생성됩니다. 더 작은 데이터 프레임을 보면 여전히 원래 데이터 프레임의 행 인덱스를 전달할 수 있습니다. 원본 인덱스가 다음과 같은 경우 숫자 , 이제 연속적이지 않은 인덱스가 있습니다.

javac가 인식되지 않습니다

인덱스 구문 재설정

구문:

DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill=)



  • 매개변수:
    • level> : 재설정할 다중 레벨 인덱스 레벨을 지정합니다.
    • drop> : True인 경우 현재 인덱스를 삭제합니다. False인 경우 새 열로 추가됩니다.
    • inplace> : True인 경우 DataFrame을 수정합니다. False인 경우 새 DataFrame을 반환합니다.
    • col_level> : 재설정할 다중 레벨 열의 레벨을 지정합니다.
    • col_fill> : 열 수준의 누락된 값을 채웁니다.
  • 반환 유형: 다음과 같은 경우 새 DataFrame을 반환합니다.inplace>거짓이다; 다음과 같은 경우 없음inplace>사실이다

음, 팬더는 reset_index()> 기능. 따라서 인덱스를 0부터 시작하는 기본 정수 인덱스로 재설정하려면 다음을 사용하면 됩니다.reset_index()>기능. 이제 DataFrame의 인덱스를 재설정할 수 있는 다양한 방법을 살펴보겠습니다.

인덱스 재설정이란 무엇입니까?

~ 안에 파이썬 프로그래밍 언어와 팬더 라이브러리,reset_index>메소드는 데이터 프레임의 인덱스를 재설정하는 데 사용됩니다. Pandas에서 DataFrame에 대한 작업을 수행하면 DataFrame의 인덱스가 변경되거나 순서가 지정되지 않을 수 있습니다. 그만큼reset_index>메소드를 사용하면 인덱스를 기본 정수 기반 인덱스로 재설정하고 팬더 데이터프레임 선택적으로 현재 인덱스를 제거합니다.

Pandas Dataframe에서 인덱스 재설정

Pandas Dataframe에서 인덱스를 재설정할 수 있는 다양한 방법이 있으며, 일반적으로 사용되는 몇 가지 방법을 예와 함께 설명합니다.

  • 기본 인덱스를 제거하지 않고 자체 인덱스 생성
  • 자신만의 인덱스 생성 및 기본 인덱스 제거
  • 자체 인덱스를 재설정하고 기본 인덱스를 인덱스로 생성
  • 데이터프레임의 열을 인덱스로 만들고 기본 인덱스 제거
  • 인덱스를 제거하지 않고 데이터 프레임의 열을 인덱스로 만들기

Pandas DataFrame 만들기

여기서는 샘플 Pandas Dataframe을 생성합니다.

파이썬3




# Import pandas package> import> pandas as pd> > # Define a dictionary containing employee data> data>=> {>'Name'>:[>'Jai'>,>'Princi'>,>'Gaurav'>,>'Anuj'>,>'Geeku'>],> >'Age'>:[>27>,>24>,>22>,>32>,>15>],> >'Address'>:[>'Delhi'>,>'Kanpur'>,>'Allahabad'>,>'Kannauj'>,>'Noida'>],> >'Qualification'>:[>'Msc'>,>'MA'>,>'MCA'>,>'Phd'>,>'10th'>] }> # Convert the dictionary into DataFrame> df>=> pd.DataFrame(data)> print>(df)>

>

>

산출:

   Name Age Address Qualification   0 Jai 27 Delhi Msc 1 Princi 24 Kanpur MA 2 Gaurav 22 Allahabad MCA 3 Anuj 32 Kannauj Phd 4 Geeku 15 Noida 10th>

만들다 기본 인덱스를 제거하지 않고 인덱스 소유

아래 예제의 코드는 pandas 라이브러리를 사용하여 직원 데이터에서 DataFrame을 생성합니다. 이는 다음을 정의합니다. 사전, 사용자 정의 인덱스를 설정하고 이를 DataFrame으로 변환한 후 인덱스를 재설정하고 결과를 인쇄합니다.

파이썬3




# Import pandas package> import> pandas as pd> > # Define a dictionary containing employee data> data>=> {>'Name'>:[>'Jai'>,>'Princi'>,>'Gaurav'>,>'Anuj'>,>'Geeku'>],> >'Age'>:[>27>,>24>,>22>,>32>,>15>],> >'Address'>:[>'Delhi'>,>'Kanpur'>,>'Allahabad'>,>'Kannauj'>,>'Noida'>],> >'Qualification'>:[>'Msc'>,>'MA'>,>'MCA'>,>'Phd'>,>'10th'>] }> index>=> [>'a'>,>'b'>,>'c'>,>'d'>,>'e'>]> # Convert the dictionary into DataFrame> df>=> pd.DataFrame(data, index)> # Make Own Index as index> # In this case default index is exist> df.reset_index(inplace>=> True>)> print>(df)>

>

>

산출:

하이브가 뭐야?
   index Name Age Address Qualification   0 a Jai 27 Delhi Msc 1 b Princi 24 Kanpur MA 2 c Gaurav 22 Allahabad MCA 3 d Anuj 32 Kannauj Phd 4 e Geeku 15 Noida 10th>

자신만의 인덱스 생성 및 기본 인덱스 제거

아래 예제에서 코드는 pandas 라이브러리를 사용하여 사전에 저장된 직원 데이터에서 DataFrame을 생성합니다. 사용자 지정 인덱스('a' ~ 'e')를 설정한 다음 결과 DataFrame을 인쇄합니다. 여기서 사용자 지정 인덱스는 기본 숫자 인덱스를 대체합니다.

파이썬3




# Import pandas package> import> pandas as pd> > # Define a dictionary containing employee data> data>=> {>'Name'>:[>'Jai'>,>'Princi'>,>'Gaurav'>,>'Anuj'>,>'Geeku'>],> >'Age'>:[>27>,>24>,>22>,>32>,>15>],> >'Address'>:[>'Delhi'>,>'Kanpur'>,>'Allahabad'>,>'Kannauj'>,>'Noida'>],> >'Qualification'>:[>'Msc'>,>'MA'>,>'MCA'>,>'Phd'>,>'10th'>] }> # Create own index> index>=> [>'a'>,>'b'>,>'c'>,>'d'>,>'e'>]> # Convert the dictionary into DataFrame> # Make Own Index and Removing Default index> df>=> pd.DataFrame(data, index)> print>(df)>

>

>

산출:

   Name Age Address Qualification   a Jai 27 Delhi Msc b Princi 24 Kanpur MA c Gaurav 22 Allahabad MCA d Anuj 32 Kannauj Phd e Geeku 15 Noida 10th>

자체 인덱스를 재설정하고 기본 인덱스를 인덱스로 생성

아래 예제의 코드는 사용자 지정 인덱스('a' ~ 'e')가 있는 직원 데이터 사전에서 Pandas DataFrame을 만듭니다. 그런 다음 인덱스를 재설정하여 사용자 정의 인덱스를 기본 숫자 인덱스로 바꾼 다음 결과 프레임을 인쇄합니다.

파이썬3




# Import pandas package> import> pandas as pd> > # Define a dictionary containing employee data> data>=> {>'Name'>:[>'Jai'>,>'Princi'>,>'Gaurav'>,>'Anuj'>,>'Geeku'>],> >'Age'>:[>27>,>24>,>22>,>32>,>15>],> >'Address'>:[>'Delhi'>,>'Kanpur'>,>'Allahabad'>,>'Kannauj'>,>'Noida'>],> >'Qualification'>:[>'Msc'>,>'MA'>,>'MCA'>,>'Phd'>,>'10th'>] }> # Create own index> index>=> [>'a'>,>'b'>,>'c'>,>'d'>,>'e'>]> # Convert the dictionary into DataFrame> df>=> pd.DataFrame(data, index)> # remove own index with default index> df.reset_index(inplace>=> True>, drop>=> True>)> print>(df)>

>

>

출력 :

   Name Age Address Qualification   0 Jai 27 Delhi Msc 1 Princi 24 Kanpur MA 2 Gaurav 22 Allahabad MCA 3 Anuj 32 Kannauj Phd 4 Geeku 15 Noida 10th>

열을 인덱스로 만들고 기본 인덱스 제거

아래 예제에서는 직원 데이터에서 Pandas DataFrame을 생성하고 사용자 정의 인덱스를 설정한 다음 기본 숫자 인덱스를 제거하면서 인덱스를 'Age' 열로 변경합니다. 최종 데이터 프레임은 두 번 인쇄됩니다.

파이썬3


내 모니터 화면 크기는 얼마야



# Import pandas package> import> pandas as pd> > # Define a dictionary containing employee data> data>=> {>'Name'>:[>'Jai'>,>'Princi'>,>'Gaurav'>,>'Anuj'>,>'Geeku'>],> >'Age'>:[>27>,>24>,>22>,>32>,>15>],> >'Address'>:[>'Delhi'>,>'Kanpur'>,>'Allahabad'>,>'Kannauj'>,>'Noida'>],> >'Qualification'>:[>'Msc'>,>'MA'>,>'MCA'>,>'Phd'>,>'10th'>] }> # Create own index> index>=> [>'a'>,>'b'>,>'c'>,>'d'>,>'e'>]> # Convert the dictionary into DataFrame> df>=> pd.DataFrame(data, index)> # set index any column of our DF and> # remove default index> df.set_index([>'Age'>], inplace>=> True>)> print>(df)>

>

>

산출:

   Name Address Qualification   Age 27 Jai Delhi Msc 24 Princi Kanpur MA 22 Gaurav Allahabad MCA 32 Anuj Kannauj Phd 15 Geeku Noida 10th>

인덱스를 제거하지 않고 데이터 프레임의 열을 인덱스로 만들기

아래 예제에서는 처음에 사용자 지정 인덱스를 사용하여 직원 데이터에서 DataFrame을 생성하는 코드가 있습니다. 그런 다음 'Age' 열을 인덱스로 설정하고 기본 숫자 인덱스를 제거하지 않고 인덱스를 재설정한 후 마지막으로 결과 DataFrame을 인쇄합니다.

파이썬3

우분투용 ipconfig




# Import pandas package> import> pandas as pd> > # Define a dictionary containing employee data> data>=> {>'Name'>:[>'Jai'>,>'Princi'>,>'Gaurav'>,>'Anuj'>,>'Geeku'>],> >'Age'>:[>27>,>24>,>22>,>32>,>15>],> >'Address'>:[>'Delhi'>,>'Kanpur'>,>'Allahabad'>,>'Kannauj'>,>'Noida'>],> >'Qualification'>:[>'Msc'>,>'MA'>,>'MCA'>,>'Phd'>,>'10th'>] }> # Create own index> index>=> [>'a'>,>'b'>,>'c'>,>'d'>,>'e'>]> # Convert the dictionary into DataFrame> df>=> pd.DataFrame(data, index)> # set any column as index> # Here we set age column as index> df.set_index([>'Age'>], inplace>=> True>)> # reset index without removing default index> df.reset_index(level>=>[>'Age'>], inplace>=> True>)> print>(df)>

>

>

산출:

    Age Name Address Qualification   0 27 Jai Delhi Msc 1 24 Princi Kanpur MA 2 22 Gaurav Allahabad MCA 3 32 Anuj Kannauj Phd 4 15 Geeku Noida 10th>