pandas categorical

pandasのcategoricalな変数の扱い方

タグ categorical one-hot encoding


pandasのcategoricalな変数の扱い方

概要

  • pandasにはカテゴリ型が存在する
    • astype("category")とすればよい
  • 面倒なワンホットエンコーディングにもデフォルトの機能として含まれる

具体例

seriesをcategory変数にして数字のインデックスを振る

s = pd.Series(["a", "b", None, "c"])
s = s.astype("category") #カテゴリ型に変換
print(s) # Categories (3, object): ['a', 'b', 'c']
# 数値のindexに変換
print(s.cat.codes)
"""
0    0
1    1
2   -1
3    2
"""

ワンホットエンコーディングしてダミー変数を得る

df = pd.DataFrame()
df["自転車の種類"] = ["ママチャリ", "ピスト", "クロス", "グラベル", "ロード"]
df["重さ"] = ["重い", "軽い", "普通", "普通", "軽い"]
res = pd.get_dummies(df, columns=["自転車の種類"], prefix=["type_is"])
display(res)
index 重さ type_is_クロス type_is_グラベル type_is_ピスト type_is_ママチャリ type_is_ロード
0 重い 0 0 0 1 0
1 軽い 0 0 1 0 0
2 普通 1 0 0 0 0
3 普通 0 1 0 0 0
4 軽い 0 0 0 0 1

Google Colab