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
from sklearn import preprocessing
lab_encoder = preprocessing.LabelEncoder()
df['column'] = lab_encoder.fit_transform(df['column'])
xxxxxxxxxx
# get all categorical columns in the dataframe
catCols = [col for col in df1.columns if df1[col].dtype=="O"]
from sklearn.preprocessing import LabelEncoder
lb_make = LabelEncoder()
for item in catCols:
df1[item] = lb_make.fit_transform(df1[item])
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
## Converting Age to numeric variable
df['Gender']=pd.get_dummies(df['Gender'],drop_first=1)
df.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)