# import necessary libraries
import pandas as pd
# suppose you have following train & test sets
X_train = pd.DataFrame({'col':['A', 'B', 'C', 'B']})
X_test = pd.DataFrame({'col':['A', 'C', 'D']})
# import necessary libraries
from sklearn.preprocessing import OneHotEncoder
# initialize one-hot encoder object with parameter
ohe = OneHotEncoder(handle_unknown='ignore')
# encode x_train using one-hot encoder
train_encode = ohe.fit_transform(X_train[['col']])
# encode x_test using one-hot encoder
test_encode = ohe.transform(X_test[['col']])
# convert the results into arrays
train_encode = train_encode.toarray()
test_encode = test_encode.toarray()
# print the results
print(f"Encoding of training set: \n{train_encode}")
print(f"\nEncoding of testing set: \n{test_encode}")