Write a Python program to create one-hot encodings of a categorical variable


Topic: Write a Python program to create one-hot encodings of a categorical variable

Solution

import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(25).reshape(5,-1), columns=list('abcde'))
df_onehot = pd.concat([pd.get_dummies(df['a']), df[list('bcde')]], axis=1)
print(df_onehot)



List all Python Programs