Để thay đổi tên chỉ mục, hãy sử dụng index.rename () trong Pandas. Đầu tiên, hãy nhập các thư viện được yêu cầu -
import pandas as pd
Tạo chỉ mục Pandas -
index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport')
Hiển thị chỉ mục Gấu trúc -
print("Pandas Index...\n",index) Đổi tên chỉ mục -
print("\nRename the index...\n",index.rename('Mode_of_Transport'))
Ví dụ
Sau đây là mã -
import pandas as pd
# Creating Pandas index
index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport')
# 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)
# Rename the index
print("\nRename the index...\n",index.rename('Mode_of_Transport')) Đầu ra
Điều này sẽ tạo ra kết quả sau -
Pandas Index... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], dtype='object', name='Transport') Number of elements in the index... 6 The dtype object... object Rename the index... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], dtype='object', name='Mode_of_Transport')