import pandas as pd
from sklearn.preprocessing import OneHotEncoder
data = {
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
df_encoded = OneHotEncoder()
df_encoded = pd.get_dummies(df, columns=['City'])
print(df_encoded)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter