Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Label Encoding in python

##We apply Label Encoding on black Friday dataset on the target column which is Species. It contains three species Iris-setosa, Iris-versicolor, Iris-virginica. 

# Import libraries
import numpy as np
import pandas as pd
 
# Importing dataset
df = pd.read_csv('../../data/blackFriday.csv')

#Cheking out the unique values in your dataset
df['Age'].unique()

# Import label encoder
from sklearn import preprocessing
 
# label_encoder object knows how to understand word labels.
label_encoder = preprocessing.LabelEncoder()
 
# Encode labels in column 'Age'.
df['Age']= label_encoder.fit_transform(df['Age'])
 
df['Age'].unique()
Comment

how to use label encoding in python


obj_df["body_style"] = obj_df["body_style"].astype('category')
obj_df.dtypes

obj_df["body_style_cat"] = obj_df["body_style"].cat.codes
obj_df.head()
Comment

PREVIOUS NEXT
Code Example
Python :: _ variable in python 
Python :: pyplot savefig 
Python :: python dequeu 
Python :: Convert two lists into a dictionary in python 
Python :: python turtle spiral 
Python :: random int python 
Python :: create a dataframe from dict 
Python :: variable in python 
Python :: python optional arguments 
Python :: python tkinter projects 
Python :: column to int pandas 
Python :: if statement in one-line for loop python 
Python :: remove multiindex pandas 
Python :: python tkinter arabic 
Python :: bold some letters of string in python 
Python :: subtract number from each element in list python 
Python :: python array index range 
Python :: python fill zeros left 
Python :: django admin override save 
Python :: python b string 
Python :: windows error message python 
Python :: open excel through python 
Python :: how to get count by using group by in python 
Python :: notna pandas 
Python :: pandas return row 
Python :: How to take space-separated integer input in Python 3 
Python :: how to merge two pandas dataframes on a column 
Python :: check if numpy array contains only duplicates 
Python :: how to use input in python 
Python :: prevent division by zero numpy 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =