Answers for "label encoding in python dataframe"

1

label encoding column pandas

import seaborn as sns 
df = sns.load_dataset('iris')
df['species'] = df['species'].astype('category').cat.codes
df.head(3)
'''sepal_length	sepal_width	petal_length	petal_width	species
	5.1					3.5			1.4					0.2			0
	4.9					3.0			1.4					0.2			0
	4.7					3.2			1.3					0.2			0'''
Posted by: Guest on September-30-2021
1

label encoding

from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
companydata.ShelveLoc = le.fit_transform(companydata.ShelveLoc)
Posted by: Guest on September-16-2020
3

transform categorical variables python

from sklearn.preprocessing import LabelEncoder

lb_make = LabelEncoder()
obj_df["make_code"] = lb_make.fit_transform(obj_df["make"])
obj_df[["make", "make_code"]].head(11)
Posted by: Guest on May-20-2020
0

label encoding of a column in python

import seaborn as sns 
df = sns.load_dataset('iris')
def encode(x):
    for i,j in enumerate(df['species'].unique()):
        if x == j:
            return i

df['species'] = df['species'].apply(lambda x:encode(x))
Posted by: Guest on September-08-2021

Python Answers by Framework

Browse Popular Code Answers by Language