Answers for "pandas encoding categorical variables"

0

how to store categorical variables in separate dataframe

df.loc[:,df.dtypes==np.object]
Posted by: Guest on March-22-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

pandas replace encode categorical

def encode_data(feature_name):
    ''' 
    This function takes feature name as a parameter and returns mapping dictionary to replace(or map) categorical data with numerical data.
    '''
    mapping_dict = {}
    unique_values = list(rain[feature_name].unique())
    for idx in range(len(unique_values)):
        mapping_dict[unique_values[idx]] = idx
    return mapping_dict

rain['RainToday'].replace({'No':0, 'Yes': 1}, inplace = True)
rain['RainTomorrow'].replace({'No':0, 'Yes': 1}, inplace = True)
rain['WindGustDir'].replace(encode_data('WindGustDir'),inplace = True)
rain['WindDir9am'].replace(encode_data('WindDir9am'),inplace = True)
rain['WindDir3pm'].replace(encode_data('WindDir3pm'),inplace = True)
rain['Location'].replace(encode_data('Location'), inplace = True)
Posted by: Guest on June-29-2021

Code answers related to "pandas encoding categorical variables"

Python Answers by Framework

Browse Popular Code Answers by Language