label encoding column pandas

Solutions on MaxInterview for label encoding column pandas by the best coders in the world

showing results for - "label encoding column pandas"
Alessandra
08 Jan 2020
1import seaborn as sns 
2df = sns.load_dataset('iris')
3df['species'] = df['species'].astype('category').cat.codes
4df.head(3)
5'''sepal_length	sepal_width	petal_length	petal_width	species
6	5.1					3.5			1.4					0.2			0
7	4.9					3.0			1.4					0.2			0
8	4.7					3.2			1.3					0.2			0'''
Claudia
07 Mar 2016
1import seaborn as sns 
2df = sns.load_dataset('iris')
3def encode(x):
4    for i,j in enumerate(df['species'].unique()):
5        if x == j:
6            return i
7
8df['species'] = df['species'].apply(lambda x:encode(x))