Answers for "label encoding column pandas"

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
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