Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert categorical variable to numeric python

# get all categorical columns in the dataframe
catCols = [col for col in df1.columns if df1[col].dtype=="O"]

from sklearn.preprocessing import LabelEncoder

lb_make = LabelEncoder()

for item in catCols:
    df1[item] = lb_make.fit_transform(df1[item])
Comment

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

how to convert contionous data into categorical data in python

pd.cut(df.Age,bins=[0,2,17,65,99],labels=['Toddler/Baby','Child','Adult','Elderly'])
# where bins is cut off points of bins for the continuous data 
# and key things here is that no. of labels is always less than 1
Comment

how to change value of categorical variable in python

df['Gender'].str[0].str.upper().map({'M':'Male', 'F':'Female'})
Comment

Converting categorical variable to numeric variable in python

## Converting Age to numeric variable

df['Gender']=pd.get_dummies(df['Gender'],drop_first=1)
df.head()
Comment

PREVIOUS NEXT
Code Example
Python :: python add all items in list 
Python :: python use variable in another file 
Python :: ParserError: Error tokenizing data. C error: Expected 1 fields in line 6, saw 3 
Python :: erase % sign in row pandas 
Python :: pandas transpose 
Python :: creating venv on vscode linux 
Python :: python warning 
Python :: move file python 
Python :: spacy load en 
Python :: pandas read first column as index 
Python :: collections counter 
Python :: how to fill a list in python 
Python :: pandas add column with constant value 
Python :: find most frequent element in an array python 
Python :: sort dictionary by value python 
Python :: module installed but not found python 
Python :: seaborn define linewidth 
Python :: python get pid of process 
Python :: python check if string is in input 
Python :: specify the number of decimals in a dataframe 
Python :: python numpy array replace nan with string 
Python :: python for loop even numbers 
Python :: python read pdf 
Python :: plt opacity hist 
Python :: pipenv with specific python version 
Python :: time a line of code python 
Python :: imblearn randomoversampler 
Python :: change colorbar size and place python 
Python :: deleting dataframe row in pandas based on column value 
Python :: selection sort python 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =