Python
 Computer >> コンピューター >  >> プログラミング >> Python

PythonPandas-入力ラベルのスライス位置を計算します


入力ラベルのスライス位置を計算するには、 index.slice_locs()を使用します 方法。まず、必要なライブラリをインポートします-

import pandas as pd

Pandasインデックスオブジェクトを作成する-

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

パンダのインデックスを表示する-

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

スライスの場所を取得します。 「スタート」は最初のラベルです。 「終わり」は終わりのラベルです

print("\nThe slice locations with start and stop...\n",index.slice_locs(start='q', end='v'))

以下はコードです-

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 the slice locations
# The "start" is the label to begin with
# The "end" is the label to end with
print("\nThe slice locations with start and stop...\n",index.slice_locs(start='q', end='v'))

出力

これにより、次の出力が生成されます-

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

Number of elements in the index...
11

The slice locations with start and stop...
(1, 7)

  1. ノームソート用のPythonプログラム

    この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −配列が与えられたので、ノームソートを使用してソートする必要があります。 アルゴリズム 1. Firstly we traverse the array from left to right. 2. Now,if the current element is larger or equal to the previous element then we traverse one step ahead 3. otherwise,if the current element is smaller than the

  2. Pythonでタプルにインデックスを付けてスライスする方法は?

    タプルにインデックスを付けたりスライスしたりするには、タプルで[]演算子を使用する必要があります。タプルにインデックスを付けるときに正の整数を指定すると、左から数えてタプルからそのインデックスがフェッチされます。負のインデックスの場合、右から数えてタプルからそのインデックスをフェッチします。 例 my_tuple = ('a', 'b', 'c', 'd') print(my_tuple[1]) print(my_tuple[-1]) 出力 これにより、出力が得られます- b d タプルの一部を取得する場合は、スライス演算子を