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

PythonPandas-インデックスのソートされたコピーを返します


インデックスのソートされたコピーを返すには、 index.sort_values()を使用します パンダのメソッド。まず、必要なライブラリをインポートします-

import pandas as pd

パンダインデックスの作成-

index = pd.Index([50, 10, 70, 95, 110, 90, 30])

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

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

インデックス値を並べ替えます。デフォルトでは、昇順で並べ替えます-

print("\nSort the index values...\n",index.sort_values())

以下はコードです-

import pandas as pd

# Creating Pandas index
index = pd.Index([50, 10, 70, 95, 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)

# Sort index values
# By default, sorts in Ascending order
print("\nSort the index values...\n",index.sort_values())

出力

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

Pandas Index...
Int64Index([50, 10, 70, 95, 110, 90, 30], dtype='int64')

Number of elements in the index...
7

The dtype object...
int64

Sort the index values...
Int64Index([10, 30, 50, 70, 90, 95, 110], dtype='int64')

  1. Python-Pandasインデックスの最小値を返します

    パンダインデックスの最小値を返すには、 index.min()を使用します 方法。まず、必要なライブラリをインポートします- import pandas as pd パンダインデックスの作成- index = pd.Index([10.5, 20.4, 40.5, 25.6, 5.7, 6.8, 30.8, 50.2]) パンダのインデックスを表示する- print("Pandas Index...\n",index) 最小値を取得- print("\nMinimum value..\n", index.min()) 例 以下はコードです-

  2. PythonPandas-IntervalArray内の各間隔の中点をインデックスとして返します

    IntervalArray内の各間隔の中点をインデックスとして返すには、 array.midを使用します 財産。最初は まず、必要なライブラリをインポートします- import pandas as pd 2つのIntervalオブジェクトを作成します。値が「both」の「closed」パラメータを使用して設定された閉じた間隔- interval1 = pd.Interval(50, 75, closed='both') interval2 = pd.Interval(65, 90, closed='both') 間隔を表示する- print("I