Python - 创建 Pandas 索引的新视图
要创建Pandas索引的新视图,请使用方法。首先,导入所需的库-index.view()
import pandas as pd
创建熊猫索引-
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
显示熊猫指数-
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 #创建Pandas索引 index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) #显示Pandas索引 print("Pandas Index...\n",index) #返回索引中的元素数 print("\nNumber of elements in the index...\n",index.size) #返回数据的dtype print("\nThe dtype object...\n",index.dtype) #创建新视图 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])输出结果
这将产生以下输出-
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