Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply LabelEncoder for a specific column in Pandas dataframe

I have a dataset loaded by dataframe where the class label needs to be encoded using LabelEncoder from scikit-learn. The column label is the class label column which has the following classes:

[‘Standing’, ‘Walking’, ‘Running’, ‘null’] 

To perform label encoding, I tried the following but it does not work. How can I fix it?

from sklearn import preprocessing import pandas as pd  df = pd.read_csv('dataset.csv', sep=',')  df.apply(preprocessing.LabelEncoder().fit_transform(df['label'])) 
like image 826
Kristofer Avatar asked May 09 '18 17:05

Kristofer


People also ask

How do I add a label to a column in pandas?

To change or rename the column labels of a DataFrame in pandas, just assign the new column labels (array) to the dataframe column names.

How does LabelEncoder work Sklearn?

Label Encoder: Sklearn provides a very efficient tool for encoding the levels of categorical features into numeric values. LabelEncoder encode labels with a value between 0 and n_classes-1 where n is the number of distinct labels. If a label repeats it assigns the same value to as assigned earlier.


2 Answers

You can try as following:

le = preprocessing.LabelEncoder() df['label'] = le.fit_transform(df.label.values) 

Or following would work too:

df['label'] = le.fit_transform(df['label']) 

It will replace original label values in dataframe with encoded labels.

like image 100
student Avatar answered Sep 23 '22 12:09

student


You can also do:

from sklearn.preprocessing import LabelEncoder le = LabelEncoder() df.col_name= le.fit_transform(df.col_name.values) 

where col_name = the feature that you want to label encode

like image 43
Darshan Jain Avatar answered Sep 24 '22 12:09

Darshan Jain