一个热编码正在删除我所有列中的 3 个值

问题描述 投票:0回答:0
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)

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

enter image description here

python pandas dataframe encoding one-hot-encoding
© www.soinside.com 2019 - 2024. All rights reserved.