how to store categorical variables in separate dataframe
df.loc[:,df.dtypes==np.object]
how to store categorical variables in separate dataframe
df.loc[:,df.dtypes==np.object]
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'})
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)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us