import pandas as pd

"""
Explanation: Title: Convert A String Categorical Variable To A Numeric Variable
Slug: convert_categorical_to_numeric
Summary: Convert A String Categorical Variable To A Numeric Variable 
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon  
Originally from: Data Origami.
import modules
End of explanation
"""


raw_data = {'patient': [1, 1, 1, 2, 2], 
        'obs': [1, 2, 3, 1, 2], 
        'treatment': [0, 1, 0, 1, 0],
        'score': ['strong', 'weak', 'normal', 'weak', 'strong']} 
df = pd.DataFrame(raw_data, columns = ['patient', 'obs', 'treatment', 'score'])
df

"""
Explanation: Create dataframe
End of explanation
"""


def score_to_numeric(x):
    if x=='strong':
        return 3
    if x=='normal':
        return 2
    if x=='weak':
        return 1

"""
Explanation: Create a function that converts all values of df['score'] into numbers
End of explanation
"""


df['score_num'] = df['score'].apply(score_to_numeric)
df

"""
Explanation: Apply the function to the score variable
End of explanation
"""


