Answers for "Convert the status column to a categorical variable. pandas"

2

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"]
Posted by: Guest on May-30-2021
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 "Convert the status column to a categorical variable. pandas"

Python Answers by Framework

Browse Popular Code Answers by Language