PythonPandas-CategoricalIndexのカテゴリを順序付けしないように設定します
CategoricalIndexのカテゴリを順序付けしないように設定するには、 as_unordered()を使用します パンダのメソッド。
まず、必要なライブラリをインポートします-
import pandas as pd
「categories」パラメータを使用して、カテゴリのカテゴリを設定します。値True-
の「ordered」パラメーターを使用して、カテゴリーを順序付きとして扱います。catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
カテゴリを順序付けしないように設定します-
print("\nCategoricalIndex unordered...\n", catIndex.as_unordered())
例
以下はコードです-
import pandas as pd
# Set the categories for the categorical using the "categories" parameter
# Treat the categorical as ordered using the "ordered" parameter with value True
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])
# Display the CategoricalIndex
print("CategoricalIndex...\n",catIndex)
# Get the categories
print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)
# Set the categories to be unordered
print("\nCategoricalIndex unordered...\n", catIndex.as_unordered()) 出力
これにより、次の出力が生成されます-
CategoricalIndex... 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') CategoricalIndex unordered... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=False, dtype='category')
-
PythonPandas-マスクで設定された値の新しいインデックスを返します
マスクで設定された値の新しいインデックスを返すには、 index.putmask()を使用します パンダのメソッド。まず、必要なライブラリをインポートします- import pandas as pd パンダインデックスの作成- index = pd.Index([5, 65, 10, 17, 75, 40]) パンダのインデックスを表示する- print("Pandas Index...\n",index) 値111-で3未満のインデックス値をマスクして配置します print("\nMask...\n",index.putmask(index &
-
Python-パンダをセットに型キャストする
パンダをSetに型キャストするには、set()を使用します。まず、DataFrameを作成しましょう- dataFrame = pd.DataFrame( { "EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'], "Zone": ['North', 'South', 'South&