Untitled
unknown
plain_text
9 months ago
1.1 kB
5
Indexable
import pandas as pd
def one_hot_encode(df, column):
"""
Performs one-hot encoding on a specified column of a DataFrame.
Parameters:
df (pd.DataFrame): The input DataFrame.
column (str): The name of the column to be one-hot encoded.
Returns:
pd.DataFrame: The DataFrame with one-hot encoding applied.
""" if column not in df.columns:
raise ValueError(f"Column '{column}' not found in DataFrame.")
# Perform one-hot encoding
one_hot = pd.get_dummies(df[column], prefix=column)
# Drop the original column and concatenate the one-hot encoded columns
df = df.drop(column, axis=1)
df = pd.concat([df, one_hot], axis=1)
return df
# Example usage
data = {
'Color': ['Red', 'Blue', 'Green', 'Blue', 'Red'],
'Size': ['S', 'M', 'L', 'XL', 'M'],
'Price': [100, 150, 200, 250, 150]
}
# Create a DataFrame
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Apply one-hot encoding to the 'Color' column
df_encoded = one_hot_encode(df, 'Color')
print("\nDataFrame after One-hot Encoding:")
print(df_encoded)
Editor is loading...
Leave a Comment