Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python Pandas - 인덱스의 요소 반복

<시간/>

인덱스의 요소를 반복하려면 index.repeat()를 사용하세요. Pandas의 메소드. 반복 횟수를 인수로 설정합니다. 먼저 필요한 라이브러리를 가져옵니다 -

pandas를 pd로 가져오기

팬더 인덱스 생성 -

index =pd.Index(['자동차', '자전거', '비행기', '선박', '트럭', '교외'], 이름 ='수송')

판다 인덱스 표시 -

print("판다 인덱스...\n",index)

인덱스 요소 반복 -

print("\n각 인덱스 요소를 두 번 반복한 결과...\n",index.repeat(2))

예시

다음은 코드입니다 -

pandas as pd# 생성하기 indexindex =pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport')# Display the Pandas indexprint("Pandas Index...\n",index)# Return the number of elements in the Indexprint("\nNumber of elements in the index...\n",index.size)# Return the dtype of the the dataprint("\nThe dtype 개체...\n",index.dtype)# indexprint("\n각 인덱스 요소를 두 번 반복한 후의 결과...\n",index.repeat(2)) 

출력

이것은 다음과 같은 출력을 생성합니다 -

Pandas Index...Index(['자동차', '자전거', '비행기', '배', '트럭', '교외'], dtype='object', name='운송')의 수 인덱스의 요소...6 dtype 개체... 각 인덱스 요소를 두 번 반복한 후의 objectResult...Index(['Car', 'Car', 'Bike', 'Bike', 'Airplane', 'Airplane', '선박', '선박', '트럭', '트럭', '교외', '교외'],dtype='object', name='수송')