display all categorical column in pandas dataframe
# get all categorical columns in the dataframe
catCols = [col for col in df.columns if df[col].dtype=="O"]
display all categorical column in pandas dataframe
# get all categorical columns in the dataframe
catCols = [col for col in df.columns if df[col].dtype=="O"]
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