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

PythonPandas-Indexオブジェクトから相対度数を返します


Indexオブジェクトから相対度数を返すには、 index.value_counts()を使用します パラメータが正規化のメソッド として 。

まず、必要なライブラリをインポートします-

import pandas as pd

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

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

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

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

value_counts()を使用して一意の値の数を取得します。パラメータ「normalize」をTrueに設定して、相対度数を取得します-

print("\nGet the relative frequency by dividing all values by the sum of values...\n", index.value_counts(normalize=True))

以下はコードです-

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)

# Get the count of unique values using value_counts()
# Set the parameter "normalize" to True to get the relative frequency
print("\nGet the relative frequency by dividing all values by the sum of values...\n", index.value_counts(normalize=True))

出力

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

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

Get the relative frequency by dividing all values by the sum of values...
50    0.222222
110   0.222222
90    0.222222
10    0.111111
70    0.111111
30    0.111111
dtype: float64

  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. Python-Pandasインデックスがオブジェクトdtypeであるかどうかを確認します

    パンダインデックスがオブジェクトdtypeであるかどうかを確認するには、 index.is_object()を使用します 方法。まず、必要なライブラリをインポートします- import pandas as pd パンダインデックスの作成- index = pd.Index(["Electronics", 6, 10.5, "Accessories", 25.6, 30]) パンダのインデックスを表示する- print("Pandas Index...\n",index) インデックス値にオブジェクトdtype-があるかどうかを