원본 인덱스에서 DataFrame을 생성하되 새 인덱스를 적용하려면 index.to_frame()을 사용하세요. 매개변수 색인 설정 거짓으로 .
먼저 필요한 라이브러리를 가져옵니다 -
import pandas as pd
판다 인덱스 생성 -
index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'],name ='Products')
판다 색인 표시
print("Pandas Index...\n",index)
새 인덱스를 적용하고 인덱스를 DataFrame으로 변환합니다. 여기에서 실제 인덱스는 다른 인덱스로 대체됩니다 -
print("\nIndex to DataFrame...\n",index.to_frame(index=False)) 예
다음은 코드입니다 -
import pandas as pd
# Creating Pandas index
index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'],name ='Products')
# Display the Pandas index
print("Pandas Index...\n",index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)
# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)
# Enforce new index and convert index to DataFrame
# Here, the actual index gets replaced by another index
print("\nIndex to DataFrame...\n",index.to_frame(index=False)) 출력
이것은 다음과 같은 출력을 생성합니다 -
Pandas Index... Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], dtype='object', name='Products') Number of elements in the index... 5 The dtype object... object Index to DataFrame... Products 0 Electronics 1 Accessories 2 Decor 3 Books 4 Toys