Python - 建立 Pandas 索引的新檢視
要建立 Pandas 索引的新檢視,請使用 index.view() 方法。首先,匯入所需的庫 −
import pandas as pd
建立 Pandas 索引 −
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
顯示 Pandas 索引 −
print("Pandas Index...\n",index)建立一個新檢視 −
res = index.view('uint8')
顯示新檢視 −
print("\nThe new view...\n",res)它共享相同的基本值 −
print("\nView for 0th index...\n",res[0])
print("\nView for 1st index...\n",res[1])示例
以下為程式碼 −
import pandas as pd
# Creating Pandas index
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
# 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)
# Create a new view
res = index.view('uint8')
# displaying the new view
print("\nThe new view...\n",res)
# shares the same underlying values
print("\nView for 0th index...\n",res[0])
print("\nView for 1st index...\n",res[1])輸出
將產生以下輸出 −
Pandas Index... Int64Index([50, 10, 70, 110, 90, 50, 110, 90, 30], dtype='int64') Number of elements in the index... 9 The dtype object... int64 The new view... [ 50 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 70 0 0 0 0 0 0 0 110 0 0 0 0 0 0 0 90 0 0 0 0 0 0 0 50 0 0 0 0 0 0 0 110 0 0 0 0 0 0 0 90 0 0 0 0 0 0 0 30 0 0 0 0 0 0 0] View for 0th index... 50 View for 1st index... 0
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP