pandas One-Hot-Encoding删除我的列中的3个值

vwhgwdsa  于 2023-05-21  发布在  其他
关注(0)|答案(1)|浏览(184)
from sklearn.preprocessing import OneHotEncoder
column_to_encode = ['Sex','Fasting_Blood_Sugar','Exercise_Angina']

# Create a nominal encoder object
encoder = OneHotEncoder(drop="first")

# Fit and transform the specified column using the encoder
encoded_column = encoder.fit_transform(df[column_to_encode])

# Convert the encoded column to a DataFrame
encoded_df = pd.DataFrame(encoded_column.toarray(), columns=encoder.get_feature_names_out(column_to_encode))

# Concatenate the encoded DataFrame with the original DataFrame
df = pd.concat([df, encoded_df], axis=1)

# Drop the original column that was encoded
df.drop(column_to_encode, axis=1, inplace=True)

我正在尝试这个代码。并且从数据集中删除三个值。请帮助我理解

vc9ivgsu

vc9ivgsu1#

您应该使用get_dummies()函数。这是你在动态编码时应该使用的函数。我建议只在使用管道时使用OneHotEncoder,它将自己工作。

import pandas as pd

data = dict(
    sex=['male','male','male','female','female','female'],
    name=['john', 'jack', 'joe', 'jeane', 'julia', 'jasmin'])
    
df = pd.DataFrame(data)

pd.get_dummies(df.sex)

pd.concat([df, pd.get_dummies(df.sex)], axis=1)

相关问题