Answers for "how to convert categorical data to numerical data in pandas"

1

panda categorical data into numerica

sex = train_dataset['Sex'].replace(['female','male'],[0,1])
print(sex)
Posted by: Guest on March-04-2021
1

pandas categorical to numeric

#this will label as one hot vectors (origin is split into 3 columns - USA, Europe, Japan and any one place will be 1 while the others are 0)
dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})
Posted by: Guest on September-03-2020
0

To convert categorical data to numerical

cat_cols = ['Item_Identifier', 'Item_Fat_Content', 'Item_Type', 'Outlet_Identifier', 
         'Outlet_Size', 'Outlet_Location_Type', 'Outlet_Type', 'Item_Type_Combined']
enc = LabelEncoder()

for col in cat_cols:
    train[col] = train[col].astype('str')
    test[col] = test[col].astype('str')
    train[col] = enc.fit_transform(train[col])
    test[col] = enc.transform(test[col])
Posted by: Guest on May-24-2021
-1

how to store categorical variables in separate dataframe

df.loc[:,df.dtypes==np.object]
Posted by: Guest on March-22-2021

Code answers related to "how to convert categorical data to numerical data in pandas"

Python Answers by Framework

Browse Popular Code Answers by Language