import pandas as pd from sklearn.preprocessing import LabelEncoder # Sample DataFrame data = {'City': ['New York', 'Los Angeles', 'Chicago', 'New York']} df = pd.DataFrame(data) # Initialize the label encoder label_encoder = LabelEncoder() # Fit and transform the 'City' column df['City_encoded'] = label_encoder.fit_transform(df['City']) print(df)
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