Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

label encoding in pandas

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

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

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

PREVIOUS NEXT
Code Example
Python :: save dataframe to csv 
Python :: colab install library 
Python :: python export multiple dataframes to excel 
Python :: python selenium clear input 
Python :: python make a list of odd numbers 
Python :: discord.py check if message has certain reaction 
Python :: import subdirectory python 
Python :: pd dataframe get column names 
Python :: pthon - progressbar 
Python :: adding numbers using python function 
Python :: py how to deactivate venv 
Python :: python random real 
Python :: argumrnt with reverse django 
Python :: show all columns pandas jupyter notebook 
Python :: enumerate python 
Python :: python async await 
Python :: python json open file 
Python :: drop row pandas 
Python :: google smtp 
Python :: Write a python program to find the most frequent word in text file 
Python :: how to kill tkinter 
Python :: completely uninstall python and all vritualenvs from mac 
Python :: how to convert string date to timestamp in python 
Python :: python print for loop one line 
Python :: print ocaml 
Python :: Adding new column to existing DataFrame in Pandas by assigning a list 
Python :: assigning values in python 
Python :: delete all files in a directory python 
Python :: pyttsx3 female voice template 
Python :: python file handling 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =