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

PythonPandas-OrderedCategoricalIndexから最大値を取得します


Ordered CategoricalIndexから最大値を取得するには、PandasのcatIndex.max()メソッドを使用します。

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

import pandas as pd

「categories」パラメータを使用して、カテゴリのカテゴリを設定します。 「ordered」パラメータを使用して、カテゴリを順序どおりに扱います-

catIndex = pd.CategoricalIndex(
   ["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]
)

カテゴリインデックスを表示する-

print("Categorical Index...\n",catIndex)

最大値を取得-

print("\nMaximum value from CategoricalIndex...\n",catIndex.max())

以下はコードです-

import pandas as pd

# CategoricalIndex can only take on a limited, and usually fixed, number of possible values.
# Set the categories for the categorical using the "categories" parameter
# Treat the categorical as ordered using the "ordered" parameter
catIndex = pd.CategoricalIndex(
   ["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]
)

# Display the Categorical Index
print("Categorical Index...\n",catIndex)

# Get the categories
print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)

# Get the max value
print("\nMaximum value from CategoricalIndex...\n",catIndex.max())

出力

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

Categorical Index...
CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')

Displaying Categories from CategoricalIndex...
Index(['p', 'q', 'r', 's'], dtype='object')

Maximum value from CategoricalIndex...
S

  1. Python-PandasのTimestampオブジェクトから平日を取得します

    Timestampオブジェクトから平日を取得するには、 timestamp.weekday()を使用します 方法。まず、必要なライブラリをインポートします- import pandas as pd import datetime パンダでタイムスタンプを設定します。タイムスタンプオブジェクトを作成する timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12)) その年の平日を取得します。平日は、月曜日==0、火曜日==1…日曜日==6の数字で表されます。 timestamp.weekday() 例 以下はコードです import

  2. Python Tkinterのチェックボックスから入力を取得するにはどうすればよいですか?

    チェックボックスウィジェットは、TrueまたはFalseの2つの値を持つ入力ウィジェットです。チェックボックスは、特定の値を検証する必要がある多くのアプリケーションで役立ちます。 チェックボックスから入力値を取得して、選択されている場合は選択された値を出力するとします。選択したチェックボックスの値を出力するには、 get()を使用できます。 方法。特定のウィジェットの入力値を返します。 例 # Import Tkinter library from tkinter import * # Create an instance of tkinter frame win = Tk() # Se