xxxxxxxxxx
sex = train_dataset['Sex'].replace(['female','male'],[0,1])
print(sex)
xxxxxxxxxx
from sklearn import preprocessing
lab_encoder = preprocessing.LabelEncoder()
df['column'] = lab_encoder.fit_transform(df['column'])
xxxxxxxxxx
import pandas as pd
# Assuming the categorical variable is in a DataFrame 'df' and column name is 'category'
df['category_numeric'] = pd.Categorical(df['category']).codes
# Print the updated DataFrame
print(df)
xxxxxxxxxx
#this will label as one hot vectors (origin is split into 3 columns - USA, Europe, Japan and any one place will be 1 while the others are 0)
dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})
xxxxxxxxxx
obj_df["body_style"] = obj_df["body_style"].astype('category')
obj_df.dtypes
xxxxxxxxxx
pd.get_dummies(obj_df, columns=["body_style", "drive_wheels"], prefix=["body", "drive"]).head()
xxxxxxxxxx
import pandas as pd
# Example DataFrame with a 'smoker' column containing 'yes' and 'no'
data = {'smoker': ['yes', 'no', 'no', 'yes', 'yes', 'no']}
df = pd.DataFrame(data)
# Using the replace method to convert 'yes' to 1 and 'no' to 0
df['smoker_encoded'] = df['smoker'].replace({'yes': 1, 'no': 0})
print(df)