LoginSignup
0
1

More than 1 year has passed since last update.

python pandasから ラベルエンコード

Posted at

ラベルエンコーディング(LabelEncoder)関数

from sklearn.preprocessing import LabelEncoderを利用し

pandas から 
columns_name カラムの列を指定して
ラベルエンコードして戻す関数を作成しました

def Label_Encoder_df(data,columns_name):
    from sklearn.preprocessing import LabelEncoder
    df=data
    df = df.dropna(subset=[columns_name])
    le = LabelEncoder()
    encoded = le.fit_transform(df[columns_name].values)
    decoded = le.inverse_transform(encoded)
    df['encoded'] = encoded
    df_out=df.drop([columns_name], axis=1)
    return df_out

使用するときは

df=Label_Encoder_df("pandasの変数","ラベルエンコードしたいカラムの列の文字列")

で使用していただけると幸いです

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1