Python Pandas - 獲取請求標籤的整數位置


要獲取 Pandas 中請求標籤的整數位置,請使用 **index.get_loc()** 方法。首先,匯入所需的庫 -

import pandas as pd

建立 Pandas 索引物件 -

index = pd.Index(list('pqrstuvwxyz'))

顯示 Pandas 索引 -

print("Pandas Index...\n",index)

從給定的索引獲取整數位置 -

print("\nDisplay integer location from given index...\n",index.get_loc('w'))
print("\nDisplay integer location from given index...\n",index.get_loc('z'))

示例

以下是程式碼 -

import pandas as pd

# create Pandas index object
index = pd.Index(list('pqrstuvwxyz'))

# 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)

# get integer location from the given index
print("\nDisplay integer location from given index...\n",index.get_loc('w'))
print("\nDisplay integer location from given index...\n",index.get_loc('z'))

輸出

這將產生以下輸出 -

Pandas Index...
Index(['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], dtype='object')

Number of elements in the index...
11

Display integer location from given index...
7

Display integer location from given index...
10

更新日期:2021-10-14

388 次瀏覽

職業生涯起步

完成課程以獲得認證

開始
廣告
© . All rights reserved.